Post by Trey Brown on Jul 9, 2014 16:59:51 GMT -5
So far we have learned to create a window that includes a label and an entry. Now I will teach you the art of StringVar() and buttons. Before reading this tutorial, please read pt. one of the python series written by l3galian and "Getting a window" as well as "Labels and Entries" so that you can get a feel for how we will be running this tutorial. Lets get started!
First we want to retrieve our previous tutorial's code, and apply it to this session.
I will start with the StringVar(). This is what we use in Tk to apply a certain variable to be constantly set to a widget. For example:
Notice how rather than using the text parameter, I used the textvariable. This is just housekeeping; it's mandatory, but don't worry about the meaning right now. Apply the above code to the previous code to make your text Label widget display "Hello World". We will now make a button that makes the text display "Goodbye World".
A button is a widget that only requires one more parameter than the Entry and Label widgets require. Here is the syntax:
Now, I know what your thinking. "Trey, how does this know what I want to change my text to with that command thingy right there?" Well, person, it doesn't. Computers only know what people tell them. They're quite stupid when you think about it. To make the button do something we need to define a function with the same name as the command. We can make the command whatever we like, as long as the function we're relying on is matching.
Place all of your functions directly below the import statements. This isn't mandatory but they don't work in many other places. Now, if you were following along your code should look like this:
When you first open the window:
Then after you click the button:
First we want to retrieve our previous tutorial's code, and apply it to this session.
from Tkinter import *
import sys
#StringVar() code goes here!
root = Tk()
root.title('Hello World')
text = Label(root, text="Hello World")
text.pack()
inp = Entry(root, text="Hello World")
inp.pack()
#Button will go here!
root.mainloop()
I will start with the StringVar(). This is what we use in Tk to apply a certain variable to be constantly set to a widget. For example:
#imports and root above this line
content = StringVar()
content.set('Hello World')
text = Label(root, textvariable=content)
text.pack()
root.mainloop()
Notice how rather than using the text parameter, I used the textvariable. This is just housekeeping; it's mandatory, but don't worry about the meaning right now. Apply the above code to the previous code to make your text Label widget display "Hello World". We will now make a button that makes the text display "Goodbye World".
A button is a widget that only requires one more parameter than the Entry and Label widgets require. Here is the syntax:
butt = Button(root, text="Change Text", command=changeTxt)
butt.pack()
Now, I know what your thinking. "Trey, how does this know what I want to change my text to with that command thingy right there?" Well, person, it doesn't. Computers only know what people tell them. They're quite stupid when you think about it. To make the button do something we need to define a function with the same name as the command. We can make the command whatever we like, as long as the function we're relying on is matching.
def changeTxt():
content.set("Goodbye World")
Place all of your functions directly below the import statements. This isn't mandatory but they don't work in many other places. Now, if you were following along your code should look like this:
from Tkinter import *
import sys
def changeTxt():
content.set('Goodbye World')
root = Tk()
root.title('Hello World')
content = StringVar()
content.set("Hello World")
text = Label(root, textvariable=content)
text.pack()
inp = Entry(root, text="Hello World")
inp.pack()
butt = Button(root, text="Change Text", command=changeTxt)
butt.pack()
root.mainloop()
When you first open the window:
Then after you click the button: