What is Tkinter?
Tkinter is Python’s de-facto standard GUI (Graphical User Interface) package. It is a thin object-oriented layer on top of Tcl/Tk.
Here is a simple Tkinter example.
The code below should generate a very simple GUI with a label, a textbox, a button that will allow you to browse a file and a Submit/Execute button.
Another thing is that when you “browse” for a file, the full path of the browsed file will be displayed in the filelocation textbox.
from Tkinter import * import tkFileDialog, tkMessagebox class App: def __init__(self, master): self.master = master #call start to initialize to create the UI elemets self.start() def start(self): self.master.title("This is the title of the 'Window'") self.now = datetime.datetime.now() #CREATE A TEXT/LABEL #create a variable with text label01 = "This is some text" #put "label01" in "self.master" which is the window/frame #then, put in the first row (row=0) and in the 2nd column (column=1), align it to "West"/"W" Label(self.master, text=label01).grid(row=0, column=0, sticky=W) #CREATE A TEXTBOX self.filelocation = Entry(self.master) self.filelocation["width"] = 60 self.filelocation.focus_set() self.filelocation.grid(row=1,column=0) #CREATE A BUTTON WITH "ASK TO OPEN A FILE" self.open_file = Button(self.master, text="Browse...", command=self.browse_file) #see: def browse_file(self) self.open_file.grid(row=1, column=1) #put it beside the filelocation textbox #CREATE RADIO BUTTONS RADIO_BUTTON = [ ("This will display A", "A"), ("This will display B","B") ] #initialize a variable to store the selected value of the radio buttons #set it to A by default self.radio_var = StringVar() self.radio_var.set("A") #create a loop to display the RADIO_BUTTON i=0 for text, item in RADIO_BUTTON: #setup each radio button. variable is set to the self.radio_var #and the value is set to the "item" in the for loop self.radio = Radiobutton(self.master, text=text, variable=radio_var, value=item) self.radio.grid(row=2, column=i) i += 1 #now for a button self.submit = Button(self.master, text="Execute!", command=self.start_processing, fg="red") self.submit.grid(row=3, column=0) def start_processing(self): #more code here def browse_file(self): #put the result in self.filename self.filename = tkFileDialog.askopenfilename(title="Open a file...") #this will set the text of the self.filelocation self.filelocation.insert(0,self.filename) root = Tk() app = App(root) root.mainloop() |
Save the code as “gui.py” and then use your command line to execute it.
$ python gui.py |
A Window should open up!
FYI, This is just a snippet from my first working Python script. You can view the rest of the source code in my Github: http://github.com/wenbert/tide_converter
Comments, suggestions or recommendations are of course welcomed!
You can find more resources here.
I’m glad you are writing about Python and I’m looking forward to some more articles about Django. I used to develope under Zend framework but I switched to Django about 6 months ago. It’s just so great and awesome, I no longer like Zend framework (and I used to love it).
Thank you Pacek. I dived into Django a few month ago but I had a hard time because I do not know Python that well yet. That is why I decided to practice a bit more with simple apps. Back to basics and I am having so much fun!
For Zend Framework, I have not dealt with it in a while. I have heard of a version 2 in the works but I have no idea on what it will be like. I still follow a lot of ZF guys on Twitter though
Also, I learned a lot from using ZF. I think realizing some of it’s flaws (both ZF and PHP I mean) is what’s making me like Python and Django.
The downside though is that I cannot afford yet to pay for a Linode to run my Python apps :-/