GUI ultimate choice: Tkinter

## GUI ultimate choice: Tkinter
       (Python GUI toolkits have a lot, before we learned EasyGui is one of the most simple one, but because EasyGui is too simple, it is suitable for receiving a stepping stone to GUI programming, )
Here Insert Picture Description
       (the Tkinter is Python's standard library tool, it is actually built on the technology of TK, TK technology was originally designed for the TCL .TCL is a well-known tool command language. However, due to TK portability and flexibility is very high, with very easy to use, so he gradually been ported to many scripting languages, including the famous Perl, Ruby and Python Python.Tkinter is the default GUI library, so, like Tkinter IDLE is designed to use and, therefore, we import directly from IDLE in tkinter, you can use the module in the default installation .tkinter Python will he compiled and then installed automatically.)

import tkinter as tk

app = tk.Tk()
app.title("FishC Demo")

theLabel = tk.Label(app,text = "我的第二个窗口程序!")
theLabel.pack()

app.mainloop()

                            Here Insert Picture Description
       (Parsing: ※ tk.Tk () in this class generates Tk instance of a top-level window, it is a top level window level, we also become the root window (root window)

                   ※ app.title ( "GUI Demo"), title, then this app is to set the title of a window object,

                   ※ tk.Label (app, text = "My second window of the program!"), Lable is the meaning of the label, is a component then Label this component is instantiated as a component object, and then assign a variable to the front to go inside, parameter, then it means that this component is placed on the app window, a text display assembly, further assembly is substantially Label is the most common components, mainly used to display text, images, icons, etc.)

                   ※ theLabel.pack (): for automatically adjusting the size and position of the component itself

                   ※theLabel.pack():是窗口的主事件循环,这基本是使用tkinter 的GUI程序的最后一行代码,因为进入了主事件循环之后,就由tkinter接管一切,一旦执行了这行代码,就不再由代码做主了,而是由GUI、 tkinter 来响应用户的输入,例如,用户按下一个按钮,tkinter感受到这个按钮被点到了,他就会调用你为这个按钮安排好的方法,由tkinter来调用你的函数

       ※进阶版
     (对于面向对象的编程语言,如果你想要写一个比较大的程序,那么你应该把代码封装起来,在面向对象的编程语言中,就是封装成类。)

import tkinter as tk

class App:
   def __init__(self,master):
       frame = tk.Frame(master)
       frame.pack()


       self.hi_there = tk.Button(frame,text = "打招呼",fg ="blue")
       self.hi_there.pack()

              
root = tk.Tk()
app = App(root)

root.mainloop()


                            Here Insert Picture Description
       (解析:首先是把 GUI 封装成类,类名为 App,root = tk.Tk(),先创建了一个root顶层窗口,然后app = App(root),实例化App这个类,把这个窗口对象作为参数传递过去,root.mainloop()就是进入主事件循环,
       接着来完成App这个类,首先有传入的参数,需要在构造方法去设置,构造方法里边添加了个frame框架,frame = tk.Frame(master),参数就是这个框架要放在哪个窗口对象,这里就是传进来的root窗口,形参叫做master,另外,框架一般是用于在复杂的布局里边,将这些组件分组的一个作用,比如这里有很多组件,有框架就可以把这一部分组件分为第一块,第二块…第一块在什么位置…第二块在什么位置,
       frame.pack()就是自动调整他的位置了,接着创建了一个按钮组件 self.hi_there = tk.Button(frame,text = “打招呼”,fg =“blue”),第一个参数就表示,这个按钮我就放在这frame这个框架里边了,然后text就是设置这个按钮的文字内容,第三个参数fg就是前景色的意思,就是把打招呼这个字体设置为蓝色的,最后也是自动调整按钮的位置,运行成功后,此时按打招呼那个按钮是没有反应,因为还没有给他绑定相应的触发事件,如下添上:)
Here Insert Picture Description
       Here Insert Picture Description
           (解析:在 self.hi_there = tk.Button(frame,text = “打招呼”,fg =“blue”,command=self.say_hi)设置按钮的时候,多加了一个参数command=self.say_hi,他就知道了当这个按钮被按下时,他就会去调用后面的方法了,)

       ※ modify the location of the buttons
           (Analysis: we can position additional parameters in the pack method inside to achieve change button, we know the pack () method will automatically adjust your position, he acquiesced will adjust to the uppermost top, this parameter a total of four position: left, right, top, bottom , can be adjusted by setting the parameters as follows :)
       Here Insert Picture Description
       ※ may be provided simultaneously pack () method padx pady and parameters setting button and the x-axis, y-axis padding
       Here Insert Picture Description

       ※ bg parameters can also be used to set the background color of the button
       Here Insert Picture Description

Published 247 original articles · won praise 116 · views 280 000 +

Guess you like

Origin blog.csdn.net/w15977858408/article/details/104141723
Recommended