Set initial size in TKinter

G

Gabor Urban

Hi,

I am quite newbie with Tkinter and I could not find the way to set the
size of the application. (I could find the method to make it
resizeable, though :)) ) Any ideas, suggestions or links to
references are wellcome.

Here is my code:

from Tkinter import *

class Application(Frame):
def say_hi(self):
self.db += 1
print 'hi there, -->> UG everyone! db = %d'%self.db

## TODO: meretezhetoseg
def createWidgets(self):
top = self.winfo_toplevel()
top.rowconfigure(0,weight = 1)
top.columnconfigure(0,weight = 1)
self.rowconfigure(0,weight = 1)
self.columnconfigure(0,weight = 1)
self.QUIT = Button(self)
self.QUIT["text"] = "QUIT"
self.QUIT["fg"] = "red"
self.QUIT["command"] = self.quit

self.QUIT.pack({"side": "left"})

self.hi_there = Button(self)
self.hi_there["text"] = "Hello",
self.hi_there["command"] = self.say_hi

self.hi_there.pack({"side": "left"})

def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()
self.db = 0

app = Application()
app.master.title('UG test')
app.mainloop()



Thx in advance...

Gabor
 
R

Rick Johnson

Hi,

I am quite newbie with Tkinter and I could not find the way to set the
size of the application.

Probably due to this haphazard coding style; why would you name an
object "Application" that is an instance of Tkinter.Frame? That is
just going to confuse people, including yourself. Choosing proper
names is very important. Would you call a cat a hat? Or a dog a frog?
Also, why did you post so much non-applicable code? Here is what you
should have posted...

from Tkinter import *
#
app = Tk()
app.title('This is an App, not a frame or a button')
app.config(width=50, height=400)
#frame1 = Frame() # Proper Naming Example.
#frame2 = Frame() # Proper Naming Example.
app.mainloop()

YWATF
 
E

Eric Brunel

Hi,

I am quite newbie with Tkinter and I could not find the way to set the
size of the application. (I could find the method to make it
resizeable, though :)) ) Any ideas, suggestions or links to
references are wellcome.

Usually, the best way is to use the geometry method on instances of Tk
or Toplevel. For example, if you have a variable named root which is the
instance of Tk, you can do:

root.geometry('500x400')

This will make the window 500 pixels wide and 400 pixels high.
Here is my code:

from Tkinter import *

class Application(Frame):
def say_hi(self):
self.db += 1
print 'hi there, -->> UG everyone! db = %d'%self.db

## TODO: meretezhetoseg
def createWidgets(self):
top = self.winfo_toplevel()
top.rowconfigure(0,weight = 1)
top.columnconfigure(0,weight = 1)
self.rowconfigure(0,weight = 1)
self.columnconfigure(0,weight = 1)
self.QUIT = Button(self)
self.QUIT["text"] = "QUIT"
self.QUIT["fg"] = "red"
self.QUIT["command"] = self.quit

self.QUIT.pack({"side": "left"})

self.hi_there = Button(self)
self.hi_there["text"] = "Hello",
self.hi_there["command"] = self.say_hi

self.hi_there.pack({"side": "left"})

def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()
self.db = 0

app = Application()
app.master.title('UG test')
app.mainloop()

Where did you find an example code looking like this? This looks like
veeeeeery old conventions for Tkinter programsŠ

For example, there's no need at all to do:

self.QUIT = Button(self)
self.QUIT["text"] = "QUIT"
self.QUIT["fg"] = "red"
self.QUIT["command"] = self.quit

This can be done in a single line:

self.QUIT = Button(self, text='QUIT', fg='red', command=self.quit)

The same goes for self.QUIT.pack({"side": "left"}). Nowadays, this is
always written self.QUIT.pack(side="left").

And you should avoid creating only an instance of Frame. This actually
creates a window, but it's a side-effect. Windows are created by
instantiating Tk for the main one, and Toplevel for all others. Having
only a Frame will cause problems later, for example if you want to add a
menu to the window: You can do so on instances of Tk or Toplevel, but
not on framesŠ

HTH
- Eric -
 
R

Rick Johnson

Where did you find an example code looking like this? This looks like
veeeeeery old conventions for Tkinter programsÅ 
[...]
And you should avoid creating only an instance of Frame. This actually
creates a window, but it's a side-effect.

Two major problems with Tkinter exposed here.

1. The tutorials are VERY old and need to be updated. There needs to
be one, and preferably only one site for Tkinter tutorials. One that
is through and complete!

2. Tkinter is designed to "auto-magically" create a root window if the
user does not do so explicitly. This is a major flaw in the design of
Tkinter and needs to be removed yesterday!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top