A
Before using Easy_gui to write the interface, feeling is too simple, so this is to learn tkinter to do.
Import Tkinter as tk# top-level window, root window app = tk. TK () App.title ("Flash demo") Thelabel = Tk. Label (app,text= "My second window program!) ") #建立一个组件 for displaying text and pictures Thelabel.pack () #用于自动调节组件尺寸和位置app. Mainloop () #窗口的主事件循环
This is the simplest interface.
Import Tkinter as Tkclass APP: def __init__ (self,master): frame = tk. Frame (master) #frame是框架 for complex layout Frame.pack (SIDE=TK. left,padx=10,pady=10) #自动调整位置, the default is top, Tk.right is located on the right #padx是在x轴的间距, Pady is on the y-axis spacing self.hi_there = tk. button (frame,text= "Hello", fg= "Blue", bg= "Pink", Command=self.say_hi) Self.hi_there.pack () def Say_hi (self): print ("hi") root = Tk. Tk () app = app (root) #将root底层窗口设置为参数root. Mainloop ()
Two
How to output images and text?
From tkinter Import *root = Tk () photo = Photoimage (file= "blackboard. png") Thelabel = Label (root, text= "\ r life is short \ r I use Python",
justify=left, #对齐方式 image=photo, #储存图片 compound=center, #混合模式, picture in the bottom, the text is directly above the picture font= ("Xingkai", 50), fg= "White", ) Thelabel.pack () Mainloop ()
Above are some simple to use, here is to set a button
From Tkinter import *def callback (): var.set ("Blow you, I don't believe it") root = Tk () frame1=frame (root) frame2=frame (root) var= Stringvar () Var.set ("Your downloaded movie has minors restricted content, \ r Please be over 18 years old and then click!") Textlabel = Label (frame1, Textvariable=var, #textvariable显示的是一个变量 justify=left, padx=10, pady=10) Textlabel.pack (side=left) #justify意为对齐方式photo = Photoimage (file= "Untitled. png") Imglabel = Label (Root,image=photo ) Imglabel.pack (side=right,padx=10,pady=10) Thebutton = button (frame2,text= "I am over 18 years old", Command=callback) Thebutton.pack () frame1.pack (padx=10,pady=10) frame2.pack (padx=10,pady=10) Mainloop ()
The Var above is set to a string variable so that we can change the output of the interface by changing the variable, and all the variables in the future are almost Var, such as in Intvar () and so on.
Three
The following describes the use of options
From tkinter Import *root = Tk () v = intvar () #设置Int变量c = Checkbutton (root,text= "click", Variable=v) #一旦按键被按的话, The cariable will change the value of v c.pack () L = Label (root,textvariable=v) L.pack () Mainloop ()
From tkinter Import *root = Tk () GIRLS = ["Xi Shi", "Marten Cicada", "Wang Zhaojun", "yang Yuhuan"]v = []for girl in GIRLS: v.append (Intvar ()) B=check button (Root,text=girl,variable=v[-1]) #v [-1] is the number in the last array selected, that is, the number B.pack (anchor=w) #anchor分为八个方向 we inserted in the previous statement , because left-justified , so West Wmainloop ()
From Tkinter import *root=tk () group = Labelframe (root,text= "The best scripting language is?") ", padx=5,pady=5) #通过LabelFrame构建一个容器部件, load the following options Group.pack (padx=10,pady=10) LANGSS = [ (" Python ", 1), (" Perl ", 2), ("Ruby", 3), ("Lua", 4)]v=intvar () #只设置一个v, guaranteed radio for Lang,num in LANGSS: b = Radiobutton (Group,text=lang , Variable=v,value=num) #,indicatoron=false) #indicatoron就是设置小圈圈 #value The meaning is to come and variable relative, if the same then display selected, otherwise, So v can only represent one option b.pack (anchor=w) Mainloop ()
Python Learning Tkinter (1-3)