By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,677 Members | 1,094 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,677 IT Pros & Developers. It's quick & easy.

Using Tkinter

P: n/a
Hello. Im a bit new to using Tkinter and im not a real pro in
programming itself... :P. Need some help here.

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.

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?

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.

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.
Thanks.
Aug 22 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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...to-tkinter.pdf
http://infohost.nmt.edu/tcc/help/pub...er/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...@gmail.comwrote:
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.

Aug 22 '08 #2

P: n/a
J-Burns wrote:
Hello. Im a bit new to using Tkinter and im not a real pro in
programming itself... :P. Need some help here.
OK, looks like you are getting direct answers, but I thought I'd
mention an easy way to experiment with Tkinter programming.

If you start Idle with the "-n" switch (*), then anything you do shares
the same "mainloop" as Idle, and your window manipulation is "live".
This means, that immediately after typing in:
>>import Tkinter
f = Tkinter.Frame()
You will see the frame f show up. You can experiment directly with
watching the effects of calls that you make in the interactive
interpretter.

(*) Easy way to do this:
On some systems, associate a button with "pythonw -m idlelib.idle -n".
On a Windows system with an Idle shortcut/button/icon already:
Copy the shortcut/button/icon
Right-click the shortcut and select the "properties" menu.
On the "General" tab of the Properties window:
Give the shortcut a nicer name (I use Idle25-n for mine).
On the "Shortcut" tab of the properties window, add a space and a -n
to the target line.
Click OK, and try out your new button.

--Scott David Daniels
Sc***********@Acm.Org
Aug 23 '08 #3

P: n/a
In article <2b**********************************@b2g2000prf.g ooglegroups.com>,
<ad******@gmail.comwrote:
>On Aug 22, 7:20 am, J-Burns <arslanbur...@gmail.comwrote:
Aug 23 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.