473,385 Members | 1,325 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Tkinter Canvas/Grid question

Is there a simple way of causing the size of a canvas to change as the
window is resized when using the Grid geometry manager? Setting
sticky='NESW', as in the following code, doesn't work though the
documentation for Grid seems to imply that it should: "sticky If a
slave's cell is larger than its requested dimensions, this option may be
used to position (or stretch) the slave within its cell. ... The sticky
option subsumes the combination of anchor and fill that is used by
pack". Or is there something wrong with my code?

Thanks,
Gary Richardson

from Tkinter import *

class Test(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
frame = Frame(parent)
self.canvas = Canvas(frame, bg='white' , width=300, height=100)
self.canvas.grid(row=0, column=0, sticky='NESW')
frame.grid()

root = Tk()
Test(root)
root.mainloop()

Jul 18 '05 #1
6 11243
Hi Gary
from Tkinter import *

class Test(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
frame = Frame(parent)
There is some confusion here: self *is* the frame, no need or use to make
another....
self.canvas = Canvas(frame, bg='white' , width=300, height=100)
There is some trick needed here: add:
self.rowconfigure(0, weight=1)
self.columnconfigure(0, weight=1)
self.canvas.grid(row=0, column=0, sticky='NESW')
frame.grid()

This packs the frame to the parent, so it limits the size of your frame in
any case
I should recommend pack in this place (which is possible, because it's
another frame)

Altogether it shall look as follows and work fine:

def __init__(self, parent):
Frame.__init__(self, parent)
self.pack(fill=BOTH,expand=1)

self.canvas = Canvas(self, bg='white' , width=300, height=100)
self.rowconfigure(0, weight=1)
self.columnconfigure(0, weight=1)
self.canvas.grid(row=0, column=0, sticky='nesw')

Jul 18 '05 #2
To expand on Michael Peuser's response, I'll explain a little more. The
grid first calculates the size requested by each item to find the
requested size for each row and column. When there are no rowspans,
the requested size for a row is the maximum of the requested sizes for
any item in that row. The same holds when talking about columns, their
weights and their widths.

Then, when there is too much or too little space, the difference is
split up among the rows according to their weight. The default weight
for a row is 0. Let total_weight be the sum of the weight of each row
with a widget, and diff be the number of pixels difference between the
height of the frame and the sum of the requested heights of its rows. If
total_weight is 0, then all rows get their requested height. Otherwise,
each row gets diff*row_weight/total_weight, with something done about
the rounding errors.

That's why the short answer to set the weight of row and column 0 to 1
works.

The other problem you may run into (I haven't run your code, but with
many levels of widget present, this may be a problem) is that each
level of the widget tree between this one containing the canvas, and the
Toplevel() or Tk() above it, must also be configured so that they will
expand to consume any extra available space. Otherwise, the condition
"when there is too much or too little space" can't be true, because the
widget won't be given a larger size than it requested.

Jeff

Jul 18 '05 #3

"Jeff Epler" <je****@unpythonic.net>

Thank you for the comprehensive explanations you gave I was to lazy to do
;-)
In addition I should like to point out a chapter from Nancy Walsh's book
"Learning Perl/Tk" where one can find a lot of information about the
different geometry managers (no prejudices against Perl I hope?)
http://www.oreilly.com/catalog/lperl...pter/ch02.html
The other problem you may run into (I haven't run your code, but with
many levels of widget present, this may be a problem) is that each
level of the widget tree between this one containing the canvas, and the
Toplevel() or Tk() above it, must also be configured so that they will
expand to consume any extra available space


This had been one of the problems, so I suggested to use an appropriate
paramtrized pack on the higher level for simplicity.

Kindly
Michael P
Jul 18 '05 #4
"Michael Peuser" <mp*****@web.de> wrote in message
news:bj*************@news.t-online.com...
[snip]
Michael, Jeff,

Thanks for your replies. I made the changes indicated above and the resizing
works just fine now. I'm still puzzled as to why pack() would be used with
the frame and grid() used with the canvas, and other widgets, I suppose. I
was under the impression that only a single geometry manager should be used
in a program. I guess the rule is: a single geometry manager must be used
within a frame and its descendants. Is that correct?

Thanks,
Gary Richardson


Jul 18 '05 #5

"Gary Richardson" <ga***@fidalgo.net>
Michael, Jeff,

Thanks for your replies. I made the changes indicated above and the resizing works just fine now. I'm still puzzled as to why pack() would be used with
the frame and grid() used with the canvas, and other widgets.
No, no! I just use pack() because it is much simpler and you made no
constraints as to how to show the frame in the toplevel.. There is a lot of
useful information in that chapter from Nacy Welsh's book

...... was under the impression that only a single geometry manager should be used in a program. I guess the rule is: a single geometry manager must be used
within a frame and its descendants. Is that correct?


Nearly. You can freely choose the g.m. in EVERY frame (but then stick to it
in THAT frame)

Kindly
Michael P


Jul 18 '05 #6
> "Gary Richardson" <ga***@fidalgo.net>
Michael, Jeff,

Thanks for your replies. I made the changes indicated above and the

resizing
works just fine now. I'm still puzzled as to why pack() would be used with
the frame and grid() used with the canvas, and other widgets.


You can manage any widget with any geometry manager. (You can even grid
a button inside of a label, but god knows why you'd want to)

Sometimes you must use "grid" to get proper layout. For instance, if
you want to create a scrolled item (canvas or text) with horizontal and
vertical scrollbars, this is the only sane way to do it.

Jul 18 '05 #7

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

Similar topics

1
by: Josh | last post by:
Caution, newbie approaching... I'm trying to come up with a very simple Tkinter test application that consists of a window with a drop-down menu bar at the top and a grid of colored rectangles...
1
by: syed_saqib_ali | last post by:
Please take a look at and run the code snippet shown below. It creates a canvas with vertical & Horizontal scroll-bars. If you shrink the window to smaller than the area of the canvas, the...
2
by: vm | last post by:
please help! I can't find anything wrong (except the result ofc ;-) This: picfile = 'logo.gif'
0
by: fxe | last post by:
Hi, I am using tkinter and I have a canvas that with several rectangles drawn on it. The rectangles need to have bindings to move and resize them. No problem here, but I also need to display a...
2
by: jim-on-linux | last post by:
py help, The file below will run as a stand alone file. It works fine as it is. But, when I call it from another module it locks my computer, The off switch is the only salvation. This...
4
by: peter | last post by:
I've come across a weird difference between the behaviour of the Tkinter checkbox in Windows and Linux. The issue became apparent in some code I wrote to display an image in a fixed size canvas...
4
by: D | last post by:
I'm sure this is a simple question to the Tkinter experts - I have a very basic Tkinter application that consists of 1 master window and buttons within that window. My problem is that, I need to...
3
by: frikk | last post by:
Hey everyone. I have been working with python for a couple years now, but just recently built my first program with a GUI. I decided to start with Tkinter since it is included with the base...
3
by: joshdw4 | last post by:
I hate to do this, but I've thoroughly exhausted google search. Yes, it's that pesky root window and I have tried withdraw to no avail. I'm assuming this is because of the methods I'm using. I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.