[Tutor] Trying out Tkinter with problems
Adam Cripps
kabads at gmail.com
Sat Feb 19 09:36:44 CET 2005
I'm trying out Tkinter as one of my first forays into GUI programming.
However, I'm having a couple of problems.
My intitial efforts can be seen at:
http://www.monkeez.org/code/python/tkinter/min.txt or here [1].
Firstly, I'm trying to grab the contents of Entry widget entry1 with a
StringVar and take that value to another function to print the value
to the command line. However, my function 'printentry' always runs
when the application is run, without the button being pressed. Then,
when I do press the button, the function isn't called. However, this
button is just the same as all the other buttons. Why is this?
Secondly, when I try to exit the app, the quit button doesn't kill the
root, window, but just the widgets. How do I reference the root window
and all, instead of just exiting the widgets?
TIA - Adam
--
http://www.monkeez.org
PGP key: 0x7111B833
[1]
#!/usr/bin/python
from Tkinter import *
import tkFont
import tkMessageBox
class Application(Frame):
def __init__(self, master=None, geometry="500x200-10x10"):
Frame.__init__(self, master)
self.grid()
self.createWidgets()
def createWidgets(self):
largeFont = tkFont.Font(family = "Verdana",size= "18", weight="bold")
arialFont = tkFont.Font(family="Arial", size="12", slant="italic")
self.quitButton = Button(self, text="Quit", command = self.exiting,
font = largeFont )
self.saveButton = Button(self, text = "save", command = self.save,
fg = "blue", activebackground="red")
self.save1Button = Button(self, text = "save1", command = self.save,
relief="groove")
self.save3Button = Button(self, text = "save3", command = self.save,
state = DISABLED)
#label = Label(self, text="This is the label text", font = arialFont)
entry = Entry(self, text="Start here")
entry.grid(column="0",row="4")
# This is the entry that I'm trying to capture
self.content = StringVar()
entry1 = Entry(self, textvariable=self.content)
entry1.grid(column="1", row="5")
#And this is the button which will capture it.
self.save2Button = Button(self, text = "Submit", command=
self.printentry(self.content.get()))
#label.grid(column=0,row=2)
self.quitButton.grid(column=0, row=0)
self.saveButton.grid(column=1, row = 0)
self.save1Button.grid(column=2, row = 1)
self.save2Button.grid(column=2, row = 5)
self.save3Button.grid(column=4, row = 1)
self.sexVar = StringVar()
self.sex = Checkbutton (self, text = "Sex:", variable=self.sexVar,
onvalue="male", offvalue="female")
self.sex.grid(column=0, row=6)
def printentry(self, event):
print "ok - the submit has been pressed - I need to get it's value"
#print "\n", event
def save(event):
print "This is a save"
print "\n event is ", event
def exiting(self):
if tkMessageBox.askokcancel("Quit", "Do you really wish to quit?"):
self.destroy()
self.exit()
app = Application()
app.master.title("Sample Application")
app.mainloop()
More information about the Tutor
mailing list