473,395 Members | 1,974 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,395 software developers and data experts.

main window in tkinter app

A short while ago someone posted that(unlike the examples) you should
use Tk as the base for your main window in tkinter apps, not Frame. Thus :

class MyMain(Frame):
def __init__(self, master):
self.root = master
self.master=master
self.createWidgets()
def createWidgets():
...
root = Tk()
app = MyMain(root)
app.master.title("Object Editor")
root.mainloop()

would become:

class MyMain(Tk):
...
...
app = MyMain()
app.title("My App")
app.mainloop()

When I try converting to this approach I run into a problem with the
__init__() method. It appears to go into an infinite loop in
tkinter.__getattr__().

If I omit __init__() I get a properly titled window, but must explicitly
call my createWidgets method from __main__.

class MyMain(Tk):
createWidgets()
...
...

app = MyMain()
app.title("My App")
app.createWidgets()
app.mainloop()

Am I missing something?

Bill
Jul 21 '05 #1
6 2419
On Mon, 18 Jul 2005 16:57:51 GMT, William Gill <no*****@gcgroup.net> wrote:
A short while ago someone posted that(unlike the examples) you should
use Tk as the base for your main window in tkinter apps, not Frame. Thus :

class MyMain(Frame):
def __init__(self, master):
self.root = master
self.master=master
self.createWidgets()
def createWidgets():
...
root = Tk()
app = MyMain(root)
app.master.title("Object Editor")
root.mainloop()

would become:

class MyMain(Tk):
...
...
app = MyMain()
app.title("My App")
app.mainloop()

When I try converting to this approach I run into a problem with the
__init__() method. It appears to go into an infinite loop in
tkinter.__getattr__().

[...]

I never ran into this problem. Can you please post a short script showing this behavior? Without knowing what you exactly do in your __init__ and createWidgets method, it's quite hard to figure out what happens...
--
python -c "print ''.join([chr(154 - ord(c)) for c in 'U(17zX(%,5.zmz5(17;8(%,5.Z65\'*9--56l7+-'])"
Jul 21 '05 #2
> I never ran into this problem. ...

O.K. That, means I probably have something else wrong. I will need to
start with a 'clean slate' instead of trying to modify existing code.
It's getting to convoluted to follow anyway after all the cobbling I've
done.

If I get a repeat of the original problem I will post the code and the
exact error message, but at least now I know It SHOULD work.

Thanks

Bill,
Eric Brunel wrote:
On Mon, 18 Jul 2005 16:57:51 GMT, William Gill <no*****@gcgroup.net> wrote:
A short while ago someone posted that(unlike the examples) you should
use Tk as the base for your main window in tkinter apps, not Frame.
Thus :

class MyMain(Frame):
def __init__(self, master):
self.root = master
self.master=master
self.createWidgets()
def createWidgets():
...
root = Tk()
app = MyMain(root)
app.master.title("Object Editor")
root.mainloop()

would become:

class MyMain(Tk):
...
...
app = MyMain()
app.title("My App")
app.mainloop()

When I try converting to this approach I run into a problem with the
__init__() method. It appears to go into an infinite loop in
tkinter.__getattr__().


[...]

I never ran into this problem. Can you please post a short script
showing this behavior? Without knowing what you exactly do in your
__init__ and createWidgets method, it's quite hard to figure out what
happens...

Jul 21 '05 #3
O.K. I tried from scratch, and the following snippet produces an
infinite loop saying:

File "C:\Python24\lib\lib-tk\Tkinter.py", line 1647, in __getattr__
return getattr(self.tk, attr)

If I comment out the __init__ method, I get the titled window, and print
out self.var ('1')
import os
from Tkinter import *

class MyApp(Tk):
var=1
def __init__(self):
pass
def getval(self):
return self.var
app = MyApp()

app.title("An App")
print app.getval()
app.mainloop()
Eric Brunel wrote:
On Mon, 18 Jul 2005 16:57:51 GMT, William Gill <no*****@gcgroup.net> wrote:
A short while ago someone posted that(unlike the examples) you should
use Tk as the base for your main window in tkinter apps, not Frame.
Thus :

class MyMain(Frame):
def __init__(self, master):
self.root = master
self.master=master
self.createWidgets()
def createWidgets():
...
root = Tk()
app = MyMain(root)
app.master.title("Object Editor")
root.mainloop()

would become:

class MyMain(Tk):
...
...
app = MyMain()
app.title("My App")
app.mainloop()

When I try converting to this approach I run into a problem with the
__init__() method. It appears to go into an infinite loop in
tkinter.__getattr__().


[...]

I never ran into this problem. Can you please post a short script
showing this behavior? Without knowing what you exactly do in your
__init__ and createWidgets method, it's quite hard to figure out what
happens...

Jul 21 '05 #4
It also seems to operate the same with or without " app.mainloop()". Is
an explicit call to mainloop needed?

William Gill wrote:
O.K. I tried from scratch, and the following snippet produces an
infinite loop saying:

File "C:\Python24\lib\lib-tk\Tkinter.py", line 1647, in __getattr__
return getattr(self.tk, attr)

If I comment out the __init__ method, I get the titled window, and print
out self.var ('1')
import os
from Tkinter import *

class MyApp(Tk):
var=1
def __init__(self):
pass
def getval(self):
return self.var
app = MyApp()

app.title("An App")
print app.getval()
app.mainloop()
Eric Brunel wrote:
On Mon, 18 Jul 2005 16:57:51 GMT, William Gill <no*****@gcgroup.net>
wrote:
A short while ago someone posted that(unlike the examples) you should
use Tk as the base for your main window in tkinter apps, not Frame.
Thus :

class MyMain(Frame):
def __init__(self, master):
self.root = master
self.master=master
self.createWidgets()
def createWidgets():
...
root = Tk()
app = MyMain(root)
app.master.title("Object Editor")
root.mainloop()

would become:

class MyMain(Tk):
...
...
app = MyMain()
app.title("My App")
app.mainloop()

When I try converting to this approach I run into a problem with the
__init__() method. It appears to go into an infinite loop in
tkinter.__getattr__().

[...]

I never ran into this problem. Can you please post a short script
showing this behavior? Without knowing what you exactly do in your
__init__ and createWidgets method, it's quite hard to figure out what
happens...

Jul 21 '05 #5
William Gill wrote:
O.K. I tried from scratch, and the following snippet produces an
infinite loop saying:

File "C:\Python24\lib\lib-tk\Tkinter.py", line 1647, in __getattr__
return getattr(self.tk, attr)

If I comment out the __init__ method, I get the titled window, and print
out self.var ('1')
import os
from Tkinter import *

class MyApp(Tk):
var=1
def __init__(self):
pass
def getval(self):
return self.var
app = MyApp()

app.title("An App")
print app.getval()
app.mainloop()


You're not calling the parent's __init__ inside your derived class. I
would point out where the Python Tutorial points out that you should do
this, but it's not in the obvious place (Classes: Inheritance).

Python does -not- automagically call parent-class __init__s for derived
classes, you must do that explicitly. Changing the definition of your
class to the following works:
class MyApp(Tk):

var=1
def __init__(self):
Tk.__init__(self)
pass
def getval(self):
return self.var

It works when you comment out __init__ because of a quirk in Python's
name resolution. As you'd logically expect, if you don't define a
function in a derived class but call it (such as instance.method()), it
will call the method from the base class.

You just proved that this works for __init__ methods also. When you
didn't define __init__ for your derived class, MyApp() called
Tk.__init__(), which Does the Right Thing in terms of setting up all the
specific Tkinter-specific members.
Jul 21 '05 #6
That does it!, thanks.

Thinking about it, when I created a derived class with an __init__
method, I overrode the base class's init. It should have been
intuitive that I needed to explicitly call baseclass.__init(self), it
wasn't. It might have hit me if the fault was related to someting in
baseclass.__init() not taking place, but the recursion loop didn't give
me a clue. Any idea why failing to init the base class caused the loop?
Bill
Christopher Subich wrote:
William Gill wrote:
O.K. I tried from scratch, and the following snippet produces an
infinite loop saying:

File "C:\Python24\lib\lib-tk\Tkinter.py", line 1647, in __getattr__
return getattr(self.tk, attr)

If I comment out the __init__ method, I get the titled window, and
print out self.var ('1')
import os
from Tkinter import *

class MyApp(Tk):
var=1
def __init__(self):
pass
def getval(self):
return self.var
app = MyApp()

app.title("An App")
print app.getval()
app.mainloop()

You're not calling the parent's __init__ inside your derived class. I
would point out where the Python Tutorial points out that you should do
this, but it's not in the obvious place (Classes: Inheritance).

Python does -not- automagically call parent-class __init__s for derived
classes, you must do that explicitly. Changing the definition of your
class to the following works:
>>> class MyApp(Tk):

var=1
def __init__(self):
Tk.__init__(self)
pass
def getval(self):
return self.var

It works when you comment out __init__ because of a quirk in Python's
name resolution. As you'd logically expect, if you don't define a
function in a derived class but call it (such as instance.method()), it
will call the method from the base class.

You just proved that this works for __init__ methods also. When you
didn't define __init__ for your derived class, MyApp() called
Tk.__init__(), which Does the Right Thing in terms of setting up all the
specific Tkinter-specific members.

Jul 21 '05 #7

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

Similar topics

4
by: Fuzzyman | last post by:
I'm having trouble implementing my GUI using Tkinter...... I've been working through the Tkinter tutorials from 'Programming Python' and am generally happy enough with the functionality and feel...
5
by: Peter Moscatt | last post by:
Ok.... I am pretty new to Python (as you may have gathered from previous posts). So now it time for another one of my ridiculous questions.... :-) When using 'Tkinter', what is used as the...
2
by: Sibylle Koczian | last post by:
Still trying to learn PyQt from a book about several Python GUI toolkits, I seem to learn first what doesn't work. The following small script seems to work, but after closing the window I get the...
0
by: Richard Townsend | last post by:
I've been experimenting with passing a window handle from a wxPython app to a Tkinter app. The Tkinter app should embed a Toplevel window containing a Canvas widget in the wxPython app's Frame (see...
1
by: Eric McRae | last post by:
I have created a somewhat complicated GUI which updates itself every 1/2 second by checking on several non-blocking sockets for data and modifying StringVars and/or canvas graphs when new...
3
by: Svennglenn | last post by:
Hi I'm creating a program in Tkinter and I would need help to create a "close button" for dialog windows. One of my typical dialog windows look like this: def show_window(self): top =...
0
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...
1
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...
4
by: mdmdmd | last post by:
Hello, I wish to collect 4 files from a user. So I have decided to use tkFileDialog askopenfilename. My problem is that after a few file selections the root window is destroyed (the whole...
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: 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...
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?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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...

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.