Using Tkinter
adam2new at gmail.com
adam2new at gmail.com
Fri Aug 22 14:46:33 EDT 2008
For references, you may use these PDF files (One URL changed since my
last time there, but it should be correct for now):
http://www.pythonware.com/media/data/an-introduction-to-tkinter.pdf
http://infohost.nmt.edu/tcc/help/pubs/tkinter/tkinter.pdf
(The first one may be useful for starting out)
I'll let you know ahead of time, my knowledge is in Python 2.5 (newer
versions, as I've heard, are becoming incompatible with older versions
-- at least, version 3.0, a.k.a. Python 3000).
I'll also let you know that this may be my first time helping someone.
On Aug 22, 7:20 am, J-Burns <arslanbur... at gmail.com> wrote:
> Problem 1:
>
> How do I make something appear on 2 separate windows using Tkinter? By
> this I mean that the format would be something like this:
>
> You have Page1 : This has 2-3 buttons on it.
> Clicking on each button opens up a new window respectively having
> something entirely different... possibly more buttons,text fields etc.
You can make a class for each that inherits from Tkinter.Toplevel or
Tkinter.Frame so you can re-create the window on the fly.
I'll code for using "import Tkinter" and not "from Tkinter import *"
and also code for using "pack()" for putting widgets on a window.
Remember that this is just a sample (it should work, but I didn't test
it).
* NEVER try to pack() one widget and grid() another in the same
"parent"
class Page1(Tkinter.Frame):
def __init__(self, parent=None):
Tkinter.Frame.__init__(self, parent)
self.make_widgets()
self.spawned_windows = {} # for holding the windows
# assumption: only one of each type of window open
# at a time
def make_widgets(self): # for putting in the widgets
self.btn1 = Tkinter.Button(self)
self.btn1["text"] = "Spawn Window 1" # or some other text
self.btn1["command"] = self.spawn_window_1
self.btn1.pack() # this alone will pack the button on the next
# available spot horizontally
# continue with the other buttons
def spawn_window_1(self): # I put a generic name here for sample
# still assuming one of each type of window open
if self.spawned_windows.has_key("window1"):
self.spawned_windows["window1"].lift() # show the window
return # already has one open
self.spawned_windows["window1"] = Page1A(self, spawn_closed,
"window1") # create
one
# if you already executed a "mainloop()" call, it should show
def spawn_closed(self, name): # a callback to say it closed
if self.spawned_windows.has_key(name):
del self.spawned_windows[name]
return True # meaning: it was found and closed
return False # meaning: it was not found
# other "def"s (functions/subroutines) here
class Page1A(Tkinter.Toplevel):
def __init__(self, master, closecmd, closearg):
Tkinter.Toplevel.__init__(self, master)
# for telling Page1 that this closed
self.closecmd = closecmd
self.closearg = closearg
self.make_widgets()
def make_widgets(self):
# *create widgets here
# now, a close button (I assume you dont need it anymore)
self.closebtn = Tkinter.Button(self)
self.closebtn["text"] = "Close window"
self.closebtn["command"] = self.destroy
self.closebtn.pack()
def destroy(self): # tell Page1 that this closed
self.closecmd(self.closearg)
Tkinter.Toplevel.destroy(self)
> Problem 2:
>
> If I have a drop down box in Pythons tkinter, is it possible that the
> entities that the drop down has are items that have been picked up
> from a notepad file via file handling?
You can just open the file once (on the script's start), when you
press a button, or every so often, that will update the drop-down box.
I don't know how you have that file set, so you may need to program
the syntax in.
Examples of how that file may be in:
* One item per line -- use "f = open(filename)" and
* "for line in f:
* # add line here"
* NOTE: requires to clear the drop-down before
* adding lines, and restoring the selected item
File
Edit
Help
* Comma separated with first one for drop-down label
* -- use a Python module available
* (I think "import csv" and use the Python Docs for help)
File,New,Open,Save,Quit
Edit,Cut,Copy,Paste
* Microsoft's .ini file style
* -- Should be found somewhere in the Python Documentation
[File]
name1a=value1a
name1b=value1b
[Edit]
name2a=value2a
namd2b=value2b
etc.
> Problem 3:
>
> I have 2 radio buttons Radio button A and radio button B. Below them I
> have I have 2 separate panels Panel A and Panel B possibly each having
> separate text fields, buttons etc in it.
>
> Is it possible that only one of them is shown depending on the radio
> button that I have selected? Meaning that if I have selected on Radio
> button A only Panel A is shown and likewise if I have selected Radio
> Button B only panel B is shown.
Python 2.5 and an up-to-date OS (WinXP had a problem in the past)
* Hide
widget.pack_forget() # if you used pack()
widget.grid_forget() # if you used grid()
widget.place_forget() # if you used the Unknown-To-Me place()
* Show
* just re-pack (etc.) it.
> I guess that's it for now. Even if any1 here knws any of these
> problems do post back asap.
> Wud b waiting for replies.
I don't know if it would all work, but I tried to help out.
> Thanks.
You're welcome.
More information about the Python-list
mailing list