fromTkinterImport* class application(Frame): def Say_hi(self): Print ' Hello ' def createwiegets(self):Self. QUIT = Button (self) self. quit["Text"] ="QUIT"Self. quit["FG"] ="Red"Self. quit["command"] = Self.quit self. Quit.pack ({"Side":"left"}) Self.hi_there = Button (self) self.hi_there["Text"] ="Hello"self.hi_there["command"] = Self.say_hi Self.hi_there.pack ({"Side":"left"}) def __init__(self,master=none):frame.__init__ (Self,master) Self.pack () self.createwiegets () root = Tk () app = Application (master = root) app.m Ainloop () Root.destroy ()
This code shows how to use Python to create a graphical interface. Using the Tkinter in Python is a library of graphical interfaces.
- Import the appropriate module:
from Tkinter import *
- Define a class that inherits a frame
class application(Frame): def Say_hi(self): Print ' Hello ' def createwiegets(self):Self. QUIT = Button (self) self. quit["Text"] ="QUIT"Self. quit["FG"] ="Red"Self. quit["command"] = Self.quit self. Quit.pack ({"Side":"left"}) Self.hi_there = Button (self) self.hi_there["Text"] ="Hello"self.hi_there["command"] = Self.say_hi Self.hi_there.pack ({"Side":"left"}) def __init__(self,master=none):frame.__init__ (Self,master) Self.pack () self.createwiegets ()
def Createwiegets ( Self): Self.QUIT=Button( Self) Self.QUIT["Text"] ="QUIT" Self.QUIT["FG"] ="Red" Self.QUIT["command"] = Self. Quit Self.QUIT. Pack ({"Side":"left" }) Self. Hi_there =Button( Self) Self. hi_there["Text"] ="Hello" Self. hi_there["command"] = Self. Say_hi Self. Hi_there.pack ({"Side":"left" })
root = Tk()app = Application(master = root)app.mainloop()root.destroy()
In the code snippet above, root is the container for the window, we create the Application object and specify its window container, turn on the message queue loop, and finally destroy the window.
Hello to the Python starter case