473,806 Members | 2,653 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tkinter - Resizing a canvas with a window

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(s ide='bottom')

self.ma.bind('< Configure>', self.resize )
def resize( self, event ):
#print '(%d, %d)' % (event.width, event.height)
self.cv.configu re( width = event.width-4, height = event.height-4 )
root = Tk()
app = testApp2(root)
root.mainloop()

Jul 27 '05 #1
2 29057
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(fil l=BOTH, expand=YES)
self.cv = Canvas(self.f, width=25, height=25, bg='red')
self.cv.pack(fi ll=BOTH, expand=YES)
self.b1 = Button( self.f, text='Hello', height=1, width=10,
padx=0, pady=1)
self.b1.pack(si de=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)

iD8DBQFC5uHTJd0 1MZaTXX0RAoMyAJ 0aJ5eGeaXM18BC7 CY7/1RQt46fqQCgg02s
vrD3TQnEeYV7V00 +gwig9E8=
=dt77
-----END PGP SIGNATURE-----

Jul 27 '05 #2
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(fil l=BOTH, expand=YES)
self.cv = Canvas(self.f, width=125, height=125, bg='red')
self.cv.pack(fi ll=BOTH, expand=YES)
self.b1 = Button( self.f, text='Hello', height=1, width=10,
padx=0, pady=1, \
command = self.howbig )
self.b1.pack(si de=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()
Jul 27 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2056
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) draw the window without any geometry setting, (2) fetch the geometry of the drawn window, (3) set the minimum window size to be this size, and then (4) reset the geometry of the window to be the size that I'd really like the window to start off...
1
1553
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 and doing the deletion by tag. No big deal. I deduce from this behavior that there is a single database behind the scenes for the Canvas implementation.
2
1871
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 pixel). window.onresize = myfunction(); What can I do to reload the page only after releasing the mouse. Thanks. Nabil Benamar.
0
3590
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 accordingly. However, what I really want to happen is that the area of the canvas that the scrollbars show (the Scrollregion) should expand as the window grows. It doesn't currently do this. although, if the window shrinks smaller than the...
2
3640
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 to control this? I have added some of what I think would be important code. html { margin: 0px; padding: 0px; height: 100%; }
2
18302
TMS
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: def __init__(self, canvas, xy, color, change): self.canvas = canvas self.id = self.canvas.create_rectangle(-10-abs(change),
4
1797
TMS
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 where the gif is? There is event.x that tells where the curser is, but how can one determine the location based on a keyboard arrow event? tms
1
6731
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 = TopLevel()", I was looking for a way for this "new_win" to always stay on top of "base_win" till I close "new_win", as a result also not allowing any selections to be made in the "base_win" . Thanks Rahul
3
7475
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 find_withtag or find_closest but it is not working, maybe im using the method on the wrong object. how do i do this? and how do i then get specifics about that object, ie the pixel-color? Exception in Tkinter callback
0
9598
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10623
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10371
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10373
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10111
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9192
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4330
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3010
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.