Member
Registered: Dec 2001
Location: /mnt/UNV/Mlkway/Earth/USA/California/Silicon Valley
Distribution: Kubuntu, Debian Buster Stable, Windoze 7
Posts: 684
Rep:
|
Python Tkinter GUI programming (newbie)
Hi there
Below you see the code I am refering to here.
Let me mention before that I am a bloody beginner and some things might be totally nonsensical. Right now I don't understand much about how Tkinter works, how frames, canvases, windows or whatever are put together, how they relate to eachother (and am looking for a place where this is exlained properly).
The thing that I am trying to do is write a musical learning tutor, a GUI that shows like 3 buttons (which all fork into 3 different parts of the program) and stay there, till you chose to return. (The below code is a trial to see how some things work, since I have no clue yet about how to erase a window again and put new buttons on it or such.)
A number is then randomly calculated and a picture is shown that prompts you to do something (in the case of musical key signatures name which one it is) and times you to see how fast you were.
Now the code below does not behave like I want it to:
1. how come the "function" signatures is executed without me pushing the button that I associated with it?
2. How come the signatures "function" is only executed once? If I push the button again, no new random number is generated, it does not run at all.
Maybe I am looking at this the whole wrong way, so I would be thankful for any pointers you might have. Probably the structure I have chosen is pretty wrong.
Markus
----- code starts ----
from Tkinter import *
import random
import tkMessageBox
class App:
def __init__(self, master):
w = Canvas(master, width=600, height=400, relief=RIDGE)
w2 = Canvas(master, width=100, height=100, relief=RIDGE)
w.pack()
w2.pack()
self.hi_there = Button(w2, text="1. Bach-man Training", command=self.bachman)
self.hi_there.pack(side=TOP)
self.button2 = Button(w2, text="2. Train Signatures", command=self.signatures(w))
self.button2.pack(side=TOP)
var_toggle = IntVar()
self.logging = Checkbutton(w2, text="logging enabled", var=var_toggle)
self.logging.pack(side=TOP)
self.button = Button(w2, text="QUIT", fg="red", command=w.quit)
self.button.pack(side=TOP)
def bachman(self):
print "Do the bachman!"
def signatures(self, w):
self.img0 = PhotoImage(file='c:\\install\\images\\0.gif')
self.img1 = PhotoImage(file='c:\\install\\images\\1.gif')
self.img2 = PhotoImage(file='c:\\install\\images\\2.gif')
self.img3 = PhotoImage(file='c:\\install\\images\\3.gif')
self.img4 = PhotoImage(file='c:\\install\\images\\4.gif')
self.img5 = PhotoImage(file='c:\\install\\images\\5.gif')
self.img6 = PhotoImage(file='c:\\install\\images\\6.gif')
self.img7 = PhotoImage(file='c:\\install\\images\\7.gif')
self.img8 = PhotoImage(file='c:\\install\\images\\-7.gif')
self.img9 = PhotoImage(file='c:\\install\\images\\-6.gif')
self.img10 = PhotoImage(file='c:\\install\\images\\-5.gif')
self.img11 = PhotoImage(file='c:\\install\\images\\-4.gif')
self.img12 = PhotoImage(file='c:\\install\\images\\-3.gif')
self.img13 = PhotoImage(file='c:\\install\\images\\-2.gif')
self.img14 = PhotoImage(file='c:\\install\\images\\-1.gif')
rand_nr = random.randint(-7,7)
print rand_nr
if rand_nr == -7:
w.create_image(150, 0, image=self.img8, anchor="ne")
elif rand_nr == -6:
w.create_image(150, 0, image=self.img9, anchor="ne")
elif rand_nr == -5:
w.create_image(150, 0, image=self.img10, anchor="ne")
elif rand_nr == -4:
w.create_image(150, 0, image=self.img11, anchor="ne")
elif rand_nr == -3:
w.create_image(150, 0, image=self.img12, anchor="ne")
elif rand_nr == -2:
w.create_image(150, 0, image=self.img13, anchor="ne")
elif rand_nr == -1:
w.create_image(150, 0, image=self.img14, anchor="ne")
elif rand_nr == 0:
w.create_image(150, 0, image=self.img0, anchor="ne")
elif rand_nr == 1:
w.create_image(150, 0, image=self.img1, anchor="ne")
elif rand_nr == 2:
w.create_image(150, 0, image=self.img2, anchor="ne")
elif rand_nr == 3:
w.create_image(150, 0, image=self.img3, anchor="ne")
elif rand_nr == 4:
w.create_image(150, 0, image=self.img4, anchor="ne")
elif rand_nr == 5:
w.create_image(150, 0, image=self.img5, anchor="ne")
elif rand_nr == 6:
w.create_image(150, 0, image=self.img6, anchor="ne")
elif rand_nr == 7:
w.create_image(150, 0, image=self.img7, anchor="ne")
master = Tk()
app = App(master)
mainloop()
Last edited by browny_amiga; 11-03-2008 at 08:23 AM.
|