|
Post by Trey Brown on Jul 8, 2014 21:23:58 GMT -5
!! Before taking this tutorial, please read the previous tutorial(Getting a window) and pt. 1 of the Python tutorials written by l3galian !! So, you know how to make a window appear. Whoopee. Now lets get into the good stuff, lets make content. Take your code from the last tutorial, (Getting a window) and apply it to this tutorial. Set up your code like this: from Tkinter import * import sys
root = Tk() root.title('Hello World')
#New code will go here
root.mainloop()
So, to add content is practically the same as making the window. Well, it is the same. You take a function and assign the result to a variable, then calling to do things later on! First we will work with labels. Labels are just text that shows up, no special features or actions. Lets get into the syntax: text = Label(root, text="Hello World") text.pack()
This assigns the "Label" function's result to the variable "text". The first parameter is asking for the window/frame to display the widget. We will get into frames later. The second parameter is asking for the text to be included in the label. The "Entry" function is basically the same. inp = Entry(root, text="Hello World") inp.pack()
So, the only difference between the two is that the entry takes input from the user that can be used for different things. We'll learn these things later on. Well, I bet your saying at this point, "Ok, ok, Trey I get it, but what is .pack?" Well, person that I don't know, I'll tell you! The pack function just takes the widget that it will be packing and packs the widget together and displays it to the screen! Final Product: from Tkinter import * import sys
root = Tk() root.title('Hello World')
text = Label(root, text="Hello World") text.pack()
inp = Entry(root, text="Hello World") inp.pack()
root.mainloop()
This is what you should see when you run this program:
|
|