Reputation: 1
I've looked at some other answers here, but I'm not understanding how to do this. This is the best I'm coming up with.
To eliminate off-subject comments, i prefer grid over pack, and I also like the widget.configure way of doing things, so that exactly one logical thing is accomplished with each line of code.
towards the end of the code, i have self.root.update() I have also left off the self. with no luck.
from tkinter import *
class Application:
def __init__(self, master):
frame1 = Frame(master)
frame1.grid(row=1, column=1)
self.btnQuit = Button(frame1)
self.btnQuit.configure(text="QUIT")
self.btnQuit.configure(fg="red")
self.btnQuit.configure(command=frame1.quit)
self.btnQuit.grid(row=1, column=1)
self.btnHi = Button(frame1)
self.btnHi.configure(text="hi there")
self.btnHi.configure(command="self.hello")
self.btnHi.grid(row=2, column=1)
self.lblMessage = Label(frame1)
self.lblMessage.grid(row=2, column=2)
def hello(self):
self.lblMessage.configure(text="hello there")
self.root.update()
root = Tk()
program = Application(root)
root.mainloop()
Upvotes: 0
Views: 1627
Reputation: 1
Please try this:
from tkinter import *
class Application:
def __init__(self, master):
frame1 = Frame(master)
frame1.grid(row=1, column=1)
self.btnQuit = Button(frame1)
self.btnQuit.configure(text="QUIT")
self.btnQuit.configure(fg="red")
self.btnQuit.configure(command=frame1.quit)
self.btnQuit.grid(row=1, column=1)
self.btnHi = Button(frame1)
self.btnHi.configure(text="hi there")
self.btnHi.configure(command=self.hello)
self.btnHi.grid(row=2, column=1)
self.lblMessage = Label(frame1)
self.lblMessage.grid(row=2, column=2)
def hello(self):
self.lblMessage.configure(text="hello there")
self.root.update()
root = Tk()`enter code here`
program = Application(root)
root.mainloop()
Upvotes: 0
Reputation: 21
As stated above:
self.btnHi.configure(command="self.hello")
should be:
self.btnHi.configure(command=self.hello)
and mind the indent of the lines. But also:
self.v = StringVar()
self.lblMessage = Label(frame1, textvariable=self.v)
In your hello method, use:
self.v.set("hello there")
Upvotes: 2
Reputation: 1025
Not clear to me what you are trying to do, but if you are trying to change the contents of a Label after it is created, then you need to use a StringVar() object. Setting that to the Label's textvariable parameter (in its constructor) will mean that the Label updates whenever you update the value of the StringVar (using its set() method).
Upvotes: 0
Reputation:
command
option a callable object - not a string to be eval
'd (which wouldn't work anyway since it'd be in a very different scope, e.g. with no/a different self
). http://infohost.nmt.edu/tcc/help/pubs/tkinter/button.html seems to confirm this.hello
inside __init__
instead of defining it at class level (as a method). You need to remove one level of indentation starting with def hello
Upvotes: 1