LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-03-2008, 08:17 AM   #1
browny_amiga
Member
 
Registered: Dec 2001
Location: /mnt/UNV/Mlkway/Earth/USA/California/Silicon Valley
Distribution: Kubuntu, Debian Buster Stable, Windoze 7
Posts: 684

Rep: Reputation: 56
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.
 
Old 11-07-2008, 07:35 AM   #2
browny_amiga
Member
 
Registered: Dec 2001
Location: /mnt/UNV/Mlkway/Earth/USA/California/Silicon Valley
Distribution: Kubuntu, Debian Buster Stable, Windoze 7
Posts: 684

Original Poster
Rep: Reputation: 56
Solution:

The problem gets discussed here:

http://www.ferg.org/thinking_in_tkinter/index.html

specifically in this point:

tt077_py.txt

This is a issue that probably every newbie will stumble over and I post this here to provide a hand.

In my example, the function
def signatures(self, res)
gets called, instead of being defined as a event binding. That is why it goes off at the beginning, without even clicking the button and then, clicking on the button does not work, because python misunderstands the whole thing.

Cheers

Markus
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Tkinter-GUI: how to make btuttons the same size? raskol Programming 1 04-06-2008 11:04 AM
C++ GUI programming ohade Programming 3 08-21-2005 09:26 AM
GUI programming with C/C++ mala fide Programming 3 01-26-2005 10:16 AM
GUI programming? cucolin@ Slackware 1 02-17-2004 06:13 PM
GUI programming unixbrain Programming 10 09-28-2003 03:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration