Professional Documents
Culture Documents
First Tkinter GUI
First Tkinter GUI
TkinterGUIBasics
FirstTkinterGUI/AsimpleWindow
Ok,soifyouopenupPython,andopenupanEditwindow(forlongercodewriting)
ifyoutypeinthisexactlyyoushouldseeasimpleGUI:
(NOTEIfyouareusinganyPython2version,youwillneedtorefertotkinterwithaCapitolT
TkintersobelowisforPython3,forpython2youtype:from Tkinter import * )
from tkinter import *
top = Tk()
top.minsize(400,400)
top.mainloop()
Nowaftereveryexample,SAVEandthenRUNyourcode.Onceyouhavesavedityoucanselect
Runfromthemenu,orhitF5ifyouhavemanywindowsopen,yoursmallnewprogrammaybe
lostbehindothers.
Iwillexplainthiscodelinebyline:
top.minsize(400,400):ThislinetellsPythonthatwewanttomakethewindowa
certainsize.400x400isafairlystandardsize.ThetwonumbersrepresentWIDTHand
HEIGHT.YoucanchangethesevaluesandplayaroundwiththesizetosuityourGUI.If
weREMOVEthisline,youwillgetaverysmallsquarewindow!Thereareotherwaysto
setthewidthandheight,butfornowthisisthesimplestwaytoexplain.
top.mainloop() :Thisisaloopthatmakesyourprogramrun.WithoutthislinePython
willnotmaketheGUIwindowappear.ThisloopmakesPythondisplayyourGUIwindow
untiltoldnotto(untilyoucloseit)
WhenyourunthisprogramfromPythonyoushouldseeasmallgreywindowwithatitlecalled
TkonthetopbaroftheGUI.
Ifyouhaveanyproblems,sendmeacopyofthespecificerror,ortrytotakeascreenshotofthe
Errormessage,andwewilltryandresolveitfromthere.
NEXT:ThenextdocumentwillbeaTkinterGUIverysimilartothisfirstone,butwewilladd
somefeatures.WewillchangethetitleandaddaQUITbuttontoexittheGUIcleanlyandeasily
withoutanyerrors!