Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #17573 > unrolled thread

Set initial size in TKinter

Started byGabor Urban <urbangabo@gmail.com>
First post2011-12-20 12:09 +0100
Last post2011-12-20 07:30 -0800
Articles 4 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Set initial size in TKinter Gabor Urban <urbangabo@gmail.com> - 2011-12-20 12:09 +0100
    Re: Set initial size in TKinter Rick Johnson <rantingrickjohnson@gmail.com> - 2011-12-20 06:37 -0800
    Re: Set initial size in TKinter Eric Brunel <eric.brunel@pragmadev.nospam.com> - 2011-12-20 16:20 +0100
      Re: Set initial size in TKinter Rick Johnson <rantingrickjohnson@gmail.com> - 2011-12-20 07:30 -0800

#17573 — Set initial size in TKinter

FromGabor Urban <urbangabo@gmail.com>
Date2011-12-20 12:09 +0100
SubjectSet initial size in TKinter
Message-ID<mailman.3861.1324379384.27778.python-list@python.org>
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

-- 
Linux is like a wigwam: no Gates, no Windows and an Apache inside.

[toc] | [next] | [standalone]


#17578

FromRick Johnson <rantingrickjohnson@gmail.com>
Date2011-12-20 06:37 -0800
Message-ID<1deb41c4-d511-4a54-a34d-90b0566d0f9e@q9g2000yqe.googlegroups.com>
In reply to#17573
On Dec 20, 5:09 am, Gabor Urban <urbang...@gmail.com> wrote:
> 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

[toc] | [prev] | [next] | [standalone]


#17583

FromEric Brunel <eric.brunel@pragmadev.nospam.com>
Date2011-12-20 16:20 +0100
Message-ID<eric.brunel-ECE825.16200820122011@news.wanadoo.fr>
In reply to#17573
In article <mailman.3861.1324379384.27778.python-list@python.org>,
 Gabor Urban <urbangabo@gmail.com> wrote:
> 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 -

[toc] | [prev] | [next] | [standalone]


#17585

FromRick Johnson <rantingrickjohnson@gmail.com>
Date2011-12-20 07:30 -0800
Message-ID<8fd4083e-ae80-40bf-824e-cc38aa5cddde@i8g2000vbh.googlegroups.com>
In reply to#17583
On Dec 20, 9:20 am, Eric Brunel <eric.bru...@pragmadev.nospam.com>
wrote:

> 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!

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web