I'm trying to get my canvas to resize to fill its frame within a window,
but I can't figure out how to handle the callback data from the window's
<Configure> properly. It has very strange behavior - resizing randomly
or growing by itself, shrinking to 0. The following works passably but
jumps around at random if you move the window and goes nuts if you add
the button (watch any wrapping):
from Tkinter import *
class testApp2:
def __init__( self, master ):
self.ma = master
self.f = Frame( self.ma )
self.f.pack()
self.cv = Canvas(self.f, width=25, height=25, bg='red')
self.cv.pack()
#self.b1 = Button( self.f, text='hello', command=None )
#self.b1.pack(side='bottom')
self.ma.bind('<Configure>', self.resize )
def resize( self, event ):
#print '(%d, %d)' % (event.width, event.height)
self.cv.configure( width = event.width-4, height = event.height-4 )
root = Tk()
app = testApp2(root)
root.mainloop() 2 28865
You should just use 'pack' properly. Namely, the fill= and expand=
parameters. In this case, you want to pack(fill=BOTH, expand=YES).
For the button, you may want to use pack(anchor=E) or anchor=W to make
it stick to one side of the window.
The additional parameters for the button (both creation and packing)
give a geometry that is closer to the standard buttons in Windows 95
/ Windows 2000. Use those or not, as you see fit.
Here's the new program:
from Tkinter import *
class testApp2:
def __init__( self, master ):
self.ma = master
self.f = Frame( self.ma )
self.f.pack(fill=BOTH, expand=YES)
self.cv = Canvas(self.f, width=25, height=25, bg='red')
self.cv.pack(fill=BOTH, expand=YES)
self.b1 = Button( self.f, text='Hello', height=1, width=10,
padx=0, pady=1)
self.b1.pack(side=BOTTOM, anchor=E, padx=4, pady=4)
root = Tk()
app = testApp2(root)
root.mainloop()
Jeff
PS thanks for including a full, runnable program in your post!
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)
iD8DBQFC5uHTJd01MZaTXX0RAoMyAJ0aJ5eGeaXM18BC7CY7/1RQt46fqQCgg02s
vrD3TQnEeYV7V00+gwig9E8=
=dt77
-----END PGP SIGNATURE-----
Thanks you very much. I found something interesting though, the canvas's
width and height properties are not updated when it is resized by its
packing. Looks like an oversight to me, but I've just demonstrated that
I don't have a complete grasp of Tk, so... I can use a Configure
callback to keep track of the values, however.
from Tkinter import *
class testApp3:
def __init__( self, master ):
self.ma = master
self.f = Frame( self.ma )
self.f.pack(fill=BOTH, expand=YES)
self.cv = Canvas(self.f, width=125, height=125, bg='red')
self.cv.pack(fill=BOTH, expand=YES)
self.b1 = Button( self.f, text='Hello', height=1, width=10,
padx=0, pady=1, \
command = self.howbig )
self.b1.pack(side=BOTTOM, anchor=S, padx=4, pady=4)
self.cv.bind('<Configure>', self.resize )
def howbig( self ):
print self.cv['width'], self.cv['height']
print self.cvw, self.cvh
def resize( self, event ):
print '(%d, %d)' % (event.width, event.height)
self.cvw, self.cvh = event.width-4, event.height-4
root = Tk()
app = testApp3(root)
root.mainloop() This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Douglas Alan |
last post by:
Is there a way to tell Tkinter that I'd like to allow a window to be
resizable, but only down to the point where no widget clipping occurs?
I know that to get the effect I want, I can always (1)...
|
by: Dave Harris |
last post by:
I derived two Frame classes and created a Canvas in each. I was extremely
surprised when frame1.canvas.delete('all') erased the image in
frame2.canvas!
I resolved my trouble by assigning tags...
|
by: Nabil Benamar |
last post by:
Hi,
can someone help me please.
I want to reload th page after resizing the window (Browser). I used the
event onresize.
The problem the server gets too many requests (on every resize: pixel by...
|
by: syed_saqib_ali |
last post by:
Below is a simple code snippet showing a Tkinter Window bearing a
canvas and 2 connected scrollbars (Vertical & Horizontal). Works fine.
When you shrink/resize the window the scrollbars adjust...
|
by: windy |
last post by:
Hello,
I find when resizing browser window my web page breaks down. When I view and resize other work the window appears to hide the page, not causing it to break apart. What settings would help...
|
by: TMS |
last post by:
Schools over!!! Now its time to play.
I would like to learn how to make objects move from one location to the next on a canvas widget. For example:
from Tkinter import *
class square:...
|
by: TMS |
last post by:
Is there any way of determining the location of a gif grom an arrow event? For example I have a canvas that has a gif that moves and I want the arrow to move the gif. But how does the arrow know...
|
by: rahulnag22 |
last post by:
Hi,
I have a Tk() window "base_win = Tk()" with multiple frames on it
having a combination of widgets. If I click on say a button widget
which launches a new top level window "new_win =...
|
by: skanemupp |
last post by:
so i load a gif onto a canvas and when i click the canvs i want to get
the color of the pixel that is clicked.
so i need to ge the object im clicking.
i was told in another thread to use...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |