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

tkinter: making widgets instance or not?

This is from the Tkinter tutorial:

from Tkinter import *

class App:

def __init__(self, master):

frame = Frame(master)
frame.pack()

self.button = Button(frame, text="QUIT", fg="red",
command=frame.quit)
self.button.pack(side=LEFT)

self.hi_there = Button(frame, text="Hello", command=self.say_hi)
self.hi_there.pack(side=LEFT)

def say_hi(self):
print "hi there, everyone!"

root = Tk()
app = App(root)
root.mainloop()

I'm wondering, why is frame created as a local variable, and the buttons
as instance variables? What is the difference? Can you make frame an
instance variable, or vice versa? (I tried it with the buttons and it
seems 'self.' isn't necessary, but is this just good practice for larger
programs?)
Jun 6 '06 #1
6 1691
John Salerno wrote:
I'm wondering, why is frame created as a local variable, and the buttons
as instance variables? What is the difference? Can you make frame an
instance variable, or vice versa?


Tkinter maintains its own widget hierarchy, and widgets don't go away
unless you explicitly destroy them (by calling "destroy" on a widget or
a widget parent), so you don't really need to keep references to them.

however, if you need to access a widget later on, it might be a good
idea to save a reference to it somewhere...

</F>

Jun 6 '06 #2
Fredrik Lundh wrote:
however, if you need to access a widget later on, it might be a good
idea to save a reference to it somewhere...


To follow up on that point, I have the following code now. I have two
questions about it:

1. Can I somehow make the passing of 'master' to the draw_entry method
automatic, so I don't have to type it each time I call the function?

2. Related to your comment, the obvious problem here is that it doesn't
save references to the text box names, so I can't access them later. Is
there still a way to automate the process like I've done, but have each
entry field have a separate name?

Thanks.

---------------

import Tkinter as tk
class App:

def __init__(self, master):
self.draw_entry(master, 'First Name:')
self.draw_entry(master, 'Last Name:')

def draw_entry(self, master, label_text):
frame = tk.Frame(master, bd=4)
frame.pack()
self.label = tk.Label(frame, text=label_text, width=10)
self.label.pack(side=tk.LEFT)
self.entry = tk.Entry(frame)
self.entry.pack(side=tk.LEFT)
root = tk.Tk()
app = App(root)
root.resizable(width=False, height=False)
root.mainloop()
Jun 6 '06 #3
John Salerno wrote:
2. Related to your comment, the obvious problem here is that it doesn't
save references to the text box names, so I can't access them later. Is
there still a way to automate the process like I've done, but have each
entry field have a separate name?


A thought about this: maybe I can pass in the name of what I'd like the
entry field to be called as another parameter, then after the entry
object is created I can say something like:

self.<myname> = self.entry

Of course, I'm not sure how to write the left side, because I assume I'd
have to pass in my new name as a string, but a string wouldn't work as
an instance variable.

Also, would there be any problems with this kind of assignment? Any
weird reference problems that makes this not really work?
Jun 6 '06 #4
On Tue, 2006-06-06 at 19:42 +0000, John Salerno wrote:
Fredrik Lundh wrote:
however, if you need to access a widget later on, it might be a good
idea to save a reference to it somewhere...


To follow up on that point, I have the following code now. I have two
questions about it:

1. Can I somehow make the passing of 'master' to the draw_entry method
automatic, so I don't have to type it each time I call the function?

2. Related to your comment, the obvious problem here is that it doesn't
save references to the text box names, so I can't access them later. Is
there still a way to automate the process like I've done, but have each
entry field have a separate name?

Thanks.

---------------

import Tkinter as tk
class App:

def __init__(self, master):
self.draw_entry(master, 'First Name:')
self.draw_entry(master, 'Last Name:')

def draw_entry(self, master, label_text):
frame = tk.Frame(master, bd=4)
frame.pack()
self.label = tk.Label(frame, text=label_text, width=10)
self.label.pack(side=tk.LEFT)
self.entry = tk.Entry(frame)
self.entry.pack(side=tk.LEFT)
root = tk.Tk()
app = App(root)
root.resizable(width=False, height=False)
root.mainloop()
--

Try this:

import Tkinter as tk

class App:

def __init__(self, master):
self.parent = master
self.entry1 = self.draw_entry('First Name:')
self.entry2 = self.draw_entry('Last Name:')

def draw_entry(self, label_text):
frame = tk.Frame(self,parent, bd=4)
frame.pack()
label = tk.Label(frame, text=label_text, width=10)
label.pack(side=tk.LEFT)
entry = tk.Entry(frame)
entry.pack(side=tk.LEFT)
return entry

root = tk.Tk()
app = App(root)
root.resizable(width=False, height=False)
root.mainloop()

--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

Jun 6 '06 #5
On Wed, 2006-06-07 at 09:27 +1000, John McMonagle wrote:
On Tue, 2006-06-06 at 19:42 +0000, John Salerno wrote:
Fredrik Lundh wrote:
however, if you need to access a widget later on, it might be a good
idea to save a reference to it somewhere...
To follow up on that point, I have the following code now. I have two
questions about it:

1. Can I somehow make the passing of 'master' to the draw_entry method
automatic, so I don't have to type it each time I call the function?

2. Related to your comment, the obvious problem here is that it doesn't
save references to the text box names, so I can't access them later. Is
there still a way to automate the process like I've done, but have each
entry field have a separate name?

Thanks.

---------------

import Tkinter as tk
class App:

def __init__(self, master):
self.draw_entry(master, 'First Name:')
self.draw_entry(master, 'Last Name:')

def draw_entry(self, master, label_text):
frame = tk.Frame(master, bd=4)
frame.pack()
self.label = tk.Label(frame, text=label_text, width=10)
self.label.pack(side=tk.LEFT)
self.entry = tk.Entry(frame)
self.entry.pack(side=tk.LEFT)
root = tk.Tk()
app = App(root)
root.resizable(width=False, height=False)
root.mainloop()
--

Try this:

import Tkinter as tk

class App:

def __init__(self, master):
self.parent = master
self.entry1 = self.draw_entry('First Name:')
self.entry2 = self.draw_entry('Last Name:')

def draw_entry(self, label_text):
frame = tk.Frame(self,parent, bd=4)


Sorry - typo. Should reas self.parent not self,parent
frame.pack()
label = tk.Label(frame, text=label_text, width=10)
label.pack(side=tk.LEFT)
entry = tk.Entry(frame)
entry.pack(side=tk.LEFT)
return entry

root = tk.Tk()
app = App(root)
root.resizable(width=False, height=False)
root.mainloop()

--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

Jun 6 '06 #6
John McMonagle wrote:
def __init__(self, master):
self.parent = master
self.entry1 = self.draw_entry('First Name:')
self.entry2 = self.draw_entry('Last Name:')


Genius! :) Looks like you solved both of my problems! Thanks!
Jun 7 '06 #7

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

Similar topics

2
by: Adonis | last post by:
I am creating some widgets by inheriting from Tkinter.Frame and populating the frame with whatever the widget will be then creating certain attributes/methods to be accessed later. My question is,...
25
by: BJörn Lindqvist | last post by:
See: http://www.wxpython.org/quotes.php. especially: "wxPython is the best and most mature cross-platform GUI toolkit, given a number of constraints. The only reason wxPython isn't the standard...
5
by: H J van Rooyen | last post by:
Hi, I am struggling to get the pack method to do what I intend. I am trying to display user input in a seperate window, along with a little description of the field, something like this: ...
32
by: Kevin Walzer | last post by:
I'm a Tcl/Tk developer who has been working, slowly, at learning Python, in part because Python has better support for certain kinds of applications that I want to develop than Tcl/Tk does....
7
by: Chris | last post by:
Hi, If a user resizes a Toplevel window, or I set a Toplevel's geometry using the geometry() method*, is there any way to have the geometry reset to that required for all the widgets? I think...
44
by: bg_ie | last post by:
Hi, I'm in the process of writing some code and noticed a strange problem while doing so. I'm working with PythonWin 210 built for Python 2.5. I noticed the problem for the last py file...
11
by: Kenneth McDonald | last post by:
Any guesses as to how many people are still using Tkinter? And can anyone direct me to good, current docs for Tkinter? Thanks, Ken
3
by: seanacais | last post by:
I'm trying to build an unknown number of repeating gui elements dynamically so I need to store the variables in a list of dictionaries. I understand that Scale "variable" name needs to be a...
1
by: viv1tyagi | last post by:
Hi everyone ! ! ! I'm just a month old in the world of Python and trying to develop an application using Tkinter in which a new window pops out on a particular button press.The code for this new...
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?
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
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
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,...
0
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,...

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.