Tkinter
We will build a simple GUI small steps at a time. Following code will build and launch a frame (the frame may not focus, so check your task bar)
import tkinter base = tkinter.Tk() base.mainloop() #you may need to omit this line in IDLE
tkinter is a module for building graphical user interfaces, packaged with Python distributable.
Tk() is a top level widget for your application
call mainloop when you are ready for your application to run
Place a button on the base that will close the base:
import tkinter import sys base = tkinter.Tk() button = tkinter.Button(base, text="close this", command=sys.exit) button.pack() base.mainloop()
pack() is a way of placing widgets on the container, we will look at them closer shortly
Show alert on a button click:
Show alert on a button click:
import tkinter from tkinter import messagebox def helloCallBack(): messagebox.showinfo( "Hello Python", "Hello World") base = tkinter.Tk() button = tkinter.Button(base, text="show message", command=helloCallBack) button.pack() base.mainloop()
Change base background colors
import tkinter from tkinter import messagebox color1="#CCFFFF" color2="#CCFFCC" currentcolor=color1 def changecolor(): global currentcolor if(currentcolor == color1): currentcolor = color2 else: currentcolor = color1 base.configure(bg=currentcolor) button.configure(text=currentcolor,highlightbackground=currentcolor ) base = tkinter.Tk() base["bg"]=currentcolor #same can be achieved through base.configure(bg=color1) base.geometry("500x500") #resize main window button = tkinter.Button(base, text=currentcolor, command=changecolor, highlightbackground=currentcolor)#frame around the button button.pack() base.mainloop()