Tkinter of Python for GUI development


GUI development

Tkinter

A lightweight cross-platform graphical user interface (GUI) development tool. It is the official standard library that comes with Python. It can be used directly after installing Python. Our common python IDLE is implemented using TKinter.

PyQt

QT is a cross-platform framework written in C++. This is a very comprehensive library.
PyQt is the Python version of the Qt library. It has more than 300 categories and nearly 6000 functions and methods. It is a multi-platform toolkit that can run on all major operating systems, including UNIX, Windows and Mac. PyQt uses dual licenses, and developers can choose between GPL and commercial licenses. Prior to this, the GPL version can only be used on Unix. Starting from version 4 of PyQt, the GPL license can be used on all supported platforms.
The biggest benefit of QT is that it has aQT DesiginerThis designer can facilitate our page layout,It can be said that in Tkinter, you need a lot of code to complete the page layout, and you only need to drag the control in QT to get it.

wxPython

wxPython is an open source software, an excellent GUI graphics library in the Python language, allowing Python programmers to easily create a complete and functional GUI user interface.
wxPython is open source and free, supports LINUX and WINDOWS, has good interface localization and complete functions. It also provides a designer wxFormbuilder similar to QT Designer, which can be said to be a compressed version of QT.


One, Label label

The code is as follows (example):

import tkinter as tk

#生成tk界面 app即主窗口
app = tk.Tk()

#修改窗口titile
app.title('pyhton开发基础')

#Label标签
#第一个参数是指附着的界面  调用pack函数显示
#无参数pack是top布局  使用side参数进行改变
tk.Label(app,text="这是我第一个Python界面").pack(side=tk.LEFT)

#进行消息处理的循环
app.mainloop()

Insert picture description here


Second, the Button label

Insert picture description here


3. Click the button to pop up a message

The code is as follows (example):

Use the parameter command to call the function say_hi

import tkinter.messagebox as messagebox
def say_hi():
    messagebox.showinfo("打招呼","同学你好!")
    
tk.Button(app,text='打招呼',command=say_hi).pack(side=tk.LEFT)
tk.Button(app,text='打招呼').pack(side=tk.LEFT)

Insert picture description here

Four, Frame frame

The role of Frame: It can be imagined as a rope to operate all the controls in the window at the same time

4.1 side与expand

Contrast with the first parameter which is app

#生成tk界面 app即主窗口
app = tk.Tk()

F1 = tk.Frame(app)
#修改窗口titile
app.title('pyhton开发基础')

#Label标签
#第一个参数是指附着的界面  调用pack函数显示
#无参数pack是top布局  使用side参数进行改变
tk.Label(F1,text="这是我第一个Python界面").pack(side=tk.TOP)
tk.Label(F1,text="这是我第一个\nPython界面",bg='red',fg='white').pack(side=tk.TOP)
#Button标签
tk.Button(F1,text='打招呼',command=say_hi).pack(side=tk.TOP)
tk.Button(F1,text='打招呼').pack(side=tk.TOP)

F1.pack(side=tk.LEFT)

Insert picture description here
Insert picture description here
Insert picture description here

#expand打开则side无效
F1.pack(side=tk.LEFT,expand=tk.YES,fill=tk.BOTH)

Insert picture description here
note: After expand is closed, side and fill may also conflict. When a conflict occurs, the default side direction is valid

#expand关闭(则fill=tk.X也失效)则side有效
F1.pack(side=tk.LEFT,expand=tk.NO,fill=tk.X)

Insert picture description here

F1.pack(side=tk.TOP,expand=tk.NO,fill=tk.X)

Insert picture description here

4.2 fill

#为了更好的展示效果使用背景green
F1 = tk.Frame(app,bg='green')
F1.pack(expand=tk.YES,fill=tk.NONE)

Insert picture description here

F1.pack(expand=tk.YES,fill=tk.X)

Insert picture description here

F1.pack(expand=tk.YES,fill=tk.Y)

Insert picture description here

F1.pack(expand=tk.YES,fill=tk.BOTH)

Insert picture description here

Five, layout

Above arepack layout (directional layout),There are alsogrid grid layout

F2 = tk.Frame(app,bg='red')
tk.Button(F2,text="A",width=5).grid(row=0,column=0)
tk.Button(F2,text="B",width=5).grid(row=0,column=1)
tk.Button(F2,text="C",width=5).grid(row=0,column=2)
tk.Button(F2,text="D",width=5).grid(row=0,column=3)

tk.Button(F2,text="A",width=5).grid(row=1,column=0)
tk.Button(F2,text="B",width=5).grid(row=1,column=1)
tk.Button(F2,text="C",width=5).grid(row=2,column=2)
tk.Button(F2,text="D",width=5).grid(row=3,column=3)
F2.pack()

Insert picture description here

to sum up

I am also the first time I have been exposed to TkinterGUI programming. The next article implements a small survey desktop software based on Tkinter.

Guess you like

Origin blog.csdn.net/HG0724/article/details/112248635