All things are difficult before they are easy.
凡事必先難后易。
01 | ?? 什么是 G U I ? \color{red}{什么是GUI?} 什么是GUI???
GUI是Graphical User Interface(圖形用戶界面)的縮寫。它是指通過圖形化的方式,為人們提供更加友好、直觀的用戶界面,使得用戶可以通過鼠標(biāo)和鍵盤等外設(shè)更加輕松、快捷地操作計(jì)算機(jī)。在GUI界面中,計(jì)算機(jī)會(huì)呈現(xiàn)出各種窗口、按鈕、菜單、對(duì)話框等控件,用戶可以通過這些控件與計(jì)算機(jī)進(jìn)行交互。常見的GUI框架有Tkinter、Qt、wxPython等。
Python提供了多種庫和框架來創(chuàng)建圖形用戶界面(GUI)。其中,最常用的是Python自帶的Tkinter庫、PyQt和wxPython等第三方庫。先從最簡單的Tkinter庫開始學(xué)習(xí)
02 | ?? 什么是 T k i n t e r ?為什么是 T k i n t e r ? \color{orange}{什么是Tkinter?為什么是Tkinter?} 什么是Tkinter?為什么是Tkinter???
-
什么是Tkinter?
Tkinter是Python中的一個(gè)標(biāo)準(zhǔn)圖形用戶界面(GUI)庫,它是Python的內(nèi)置庫,可以方便地創(chuàng)建窗口應(yīng)用程序并為其添加用戶界面。
Tkinter提供了各種控件(如按鈕、文本框、標(biāo)簽等)和布局選項(xiàng),可以輕松地創(chuàng)建各種復(fù)雜的GUI界面。同時(shí),Tkinter庫還提供了繪圖功能、文件對(duì)話框等實(shí)用工具,使得可以在應(yīng)用中添加更多的功能和交互性,讓應(yīng)用更加出色。
使用Tkinter庫,可以創(chuàng)建漂亮而功能強(qiáng)大的桌面應(yīng)用程序,包括游戲、工具、圖像編輯器等。它是Python開發(fā)中非常重要的一部分,應(yīng)用廣泛。
-
為什么學(xué)習(xí)Tkinter?
由于 Tkinter 是內(nèi)置到 python 的安裝包中、只要安裝好 Python 之后就能 import Tkinter 庫進(jìn)行使用,不用多余的環(huán)境配置步驟,講究的就是一個(gè)便捷 -
怎么安裝Tkinter?
pip install tkinter
03 | ?? 了解 T k i n t e r 基礎(chǔ)知識(shí) \color{yellow}{了解Tkinter基礎(chǔ)知識(shí)} 了解Tkinter基礎(chǔ)知識(shí)??
想要學(xué)習(xí)tkinter,我們首先需要了解它的基礎(chǔ)知識(shí),包括如何創(chuàng)建窗口、如何添加組件、如何設(shè)置布局等,常用的基本步驟如下
-
導(dǎo)入模塊:要使用tkinter庫,需要先導(dǎo)入它,
import tkinter
表示導(dǎo)入tkinter庫。import tkinter as tk
-
創(chuàng)建主窗口:在tkinter中,所有圖形用戶界面組件都必須是某個(gè)容器的子組件。通常,最頂層的容器是主窗口,可以用如下代碼創(chuàng)建:
root = tkinter.Tk()
這條語句創(chuàng)建了一個(gè)名為
root
的窗口。 -
設(shè)置窗口標(biāo)題:通過調(diào)用
title()
方法,可以設(shè)置主窗口的標(biāo)題,例如:root.title("窗口標(biāo)題")
這將顯示一個(gè)標(biāo)題為“窗口標(biāo)題”的窗口。
-
設(shè)置窗口尺寸:可以調(diào)用
geometry()
方法來設(shè)置主窗口的大小,例如:root.geometry("400x300")
這將創(chuàng)建一個(gè)寬度為400像素,高度為300像素的窗口。
-
添加組件:在tkinter中,可以通過創(chuàng)建各種組件來構(gòu)建GUI界面。例如按鈕、標(biāo)簽、文本框等。可以使用如下代碼創(chuàng)建一個(gè)按鈕:
btn = tkinter.Button(root, text="按鈕文本")
這將創(chuàng)建一個(gè)名為
btn
的按鈕,并將其作為主窗口的子組件展示出來。 -
組件布局:要將組件放置在GUI窗口中,可以使用
pack()
、place()
或grid()
等方法。例如:btn.pack()
這將使按鈕出現(xiàn)在主窗口的默認(rèn)位置。
-
顯示窗口:要顯示GUI窗口,需要調(diào)用
mainloop()
方法,例如:root.mainloop()
這將在屏幕上顯示主窗口并監(jiān)聽事件,直到主窗口被關(guān)閉。
-
處理事件:在tkinter中,事件是用戶輸入或系統(tǒng)觸發(fā)的動(dòng)作,例如鍵盤輸入或鼠標(biāo)點(diǎn)擊??梢允褂?code>bind()方法將事件函數(shù)綁定到指定組件的特定事件上,例如:
btn.bind('<Button-1>', handle_btn_click)
這表示將
handle_btn_click()
函數(shù)與按鈕的左鍵單擊事件綁定起來。
除此之外,tkinter還提供了很多常用組件,例如菜單、對(duì)話框、畫布等,之后再進(jìn)一步學(xué)習(xí)。
04 | ?? T k i n t e r 示例學(xué)習(xí) \color{green}{Tkinter示例學(xué)習(xí)} Tkinter示例學(xué)習(xí)??
-
以下是一個(gè)簡單的示例,演示了如何使用tkinter創(chuàng)建一個(gè)基本的窗口:
import tkinter as tk # 創(chuàng)建窗口 window = tk.Tk() window.title("My Window") window.geometry("300x200") # 添加標(biāo)簽 label = tk.Label(window, text="Hello World!") label.pack() # 進(jìn)入消息循環(huán) window.mainloop()
通過上述代碼,我們可以創(chuàng)建一個(gè)300x200的窗口,添加一個(gè)標(biāo)簽,并在窗口中顯示出來。接下來,我們需要深入學(xué)習(xí)tkinter的各種組件和布局。
-
掌握常用組件和布局方式
tkinter提供了各種GUI組件,例如按鈕、文本框、選擇框、滾動(dòng)條等。在實(shí)際開發(fā)中,我們需要靈活運(yùn)用這些組件,以滿足不同的需求。以下是一個(gè)示例代碼,展示了如何使用Frame和Button組件:import tkinter as tk window = tk.Tk() window.title("My Window") window.geometry("300x200") # 創(chuàng)建Frame frame = tk.Frame(window) frame.pack() # 添加按鈕 btn1 = tk.Button(frame, text="Button 1") btn1.pack(side=tk.LEFT) btn2 = tk.Button(frame, text="Button 2") btn2.pack(side=tk.LEFT) btn3 = tk.Button(frame, text="Button 3") btn3.pack(side=tk.LEFT) window.mainloop()
上述代碼創(chuàng)建了一個(gè)300x200的窗口,包含一個(gè)Frame和三個(gè)按鈕。按鈕被添加到Frame中,并通過設(shè)置side參數(shù)來實(shí)現(xiàn)水平排列。掌握常用組件和布局方式是學(xué)習(xí)tkinter的關(guān)鍵所在。
-
學(xué)習(xí)事件處理機(jī)制
在GUI程序中,常常需要響應(yīng)用戶的交互操作。例如點(diǎn)擊按鈕、輸入文本等都會(huì)觸發(fā)相應(yīng)的事件。我們需要了解tkinter的事件處理機(jī)制,并學(xué)會(huì)如何編寫事件處理函數(shù)。以下是一個(gè)示例代碼,演示了如何響應(yīng)按鈕的點(diǎn)擊事件:import tkinter as tk window = tk.Tk() window.title("My Window") window.geometry("300x200") def on_btn_click(): label.config(text="Button Clicked!") # 添加標(biāo)簽 label = tk.Label(window, text="Hello World!") label.pack() # 添加按鈕 btn = tk.Button(window, text="Click Me", command=on_btn_click) btn.pack() window.mainloop()
在上述代碼中,我們定義了一個(gè)名為on_btn_click的函數(shù),該函數(shù)會(huì)在按鈕被點(diǎn)擊時(shí)觸發(fā)。通過設(shè)置按鈕的command參數(shù),我們將按鈕和該函數(shù)關(guān)聯(lián)。當(dāng)按鈕被點(diǎn)擊時(shí),就會(huì)調(diào)用該函數(shù),并在標(biāo)簽中顯示相應(yīng)的文本。
通過示例,學(xué)習(xí)了如何使用tkinter創(chuàng)建窗口、添加組件、設(shè)置布局,以及如何編寫事件處理函數(shù),下面來簡單的練習(xí)一下吧
05 | ?? 經(jīng)典練習(xí):實(shí)現(xiàn)一個(gè)簡單的計(jì)算器 \color{blue}{經(jīng)典練習(xí):實(shí)現(xiàn)一個(gè)簡單的計(jì)算器} 經(jīng)典練習(xí):實(shí)現(xiàn)一個(gè)簡單的計(jì)算器??
下面是實(shí)現(xiàn)計(jì)算器的詳細(xì)步驟:
-
導(dǎo)入Tkinter庫
from tkinter import *
-
創(chuàng)建主窗口對(duì)象
window = Tk() window.title("計(jì)算器")
-
定義布局
result = Entry(window, width=33, borderwidth=5) result.grid(row=0, column=0, columnspan=4, padx=10, pady=10) button_1 = Button(window, text="1", padx=40, pady=20, command=lambda: button_click(1)) button_2 = Button(window, text="2", padx=40, pady=20, command=lambda: button_click(2)) button_3 = Button(window, text="3", padx=40, pady=20, command=lambda: button_click(3)) button_4 = Button(window, text="4", padx=40, pady=20, command=lambda: button_click(4)) button_5 = Button(window, text="5", padx=40, pady=20, command=lambda: button_click(5)) button_6 = Button(window, text="6", padx=40, pady=20, command=lambda: button_click(6)) button_7 = Button(window, text="7", padx=40, pady=20, command=lambda: button_click(7)) button_8 = Button(window, text="8", padx=40, pady=20, command=lambda: button_click(8)) button_9 = Button(window, text="9", padx=40, pady=20, command=lambda: button_click(9)) button_0 = Button(window, text="0", padx=40, pady=20, command=lambda: button_click(0)) button_add = Button(window, text="+", padx=39, pady=20, command=button_add) button_subtract = Button(window, text="-", padx=41, pady=20, command=button_subtract) button_multiply = Button(window, text="*", padx=40, pady=20, command=button_multiply) button_divide = Button(window, text="/", padx=41, pady=20, command=button_divide) button_clear = Button(window, text="清空", padx=79, pady=20, command=button_clear) button_equals = Button(window, text="=", padx=91, pady=20, command=button_equals)
-
創(chuàng)建按鈕操作函數(shù)
def button_click(number): current = result.get() result.delete(0, END) result.insert(0, str(current) + str(number)) def button_clear(): result.delete(0, END) def button_add(): first_number = result.get() global f_num global math math = "addition" f_num = int(first_number) result.delete(0, END) def button_subtract(): first_number = result.get() global f_num global math math = "subtraction" f_num = int(first_number) result.delete(0, END) def button_multiply(): first_number = result.get() global f_num global math math = "multiplication" f_num = int(first_number) result.delete(0, END) def button_divide(): first_number = result.get() global f_num global math math = "division" f_num = int(first_number) result.delete(0, END) def button_equals(): second_number = result.get() result.delete(0, END) if math == "addition": result.insert(0, f_num + int(second_number)) if math == "subtraction": result.insert(0, f_num - int(second_number)) if math == "multiplication": result.insert(0, f_num * int(second_number)) if math == "division": result.insert(0, f_num / int(second_number))
-
定義按鈕位置和樣式
button_1.grid(row=3, column=0) button_2.grid(row=3, column=1) button_3.grid(row=3, column=2) button_4.grid(row=2, column=0) button_5.grid(row=2, column=1) button_6.grid(row=2, column=2) button_7.grid(row=1, column=0) button_8.grid(row=1, column=1) button_9.grid(row=1, column=2) button_0.grid(row=4, column=0) button_clear.grid(row=4, column=1, columnspan=2) button_add.grid(row=5, column=0) button_subtract.grid(row=6, column=0) button_multiply.grid(row=6, column=1) button_divide.grid(row=6, column=2) button_equals.grid(row=5, column=1, columnspan=2)
-
運(yùn)行主循環(huán)
window.mainloop()
下面是完整代碼:
from tkinter import *
window = Tk()
window.title("計(jì)算器")
result = Entry(window, width=33, borderwidth=5)
result.grid(row=0, column=0, columnspan=4, padx=10, pady=10)
def button_click(number):
current = result.get()
result.delete(0, END)
result.insert(0, str(current) + str(number))
def button_clear():
result.delete(0, END)
def button_add():
first_number = result.get()
global f_num
global math
math = "addition"
f_num = int(first_number)
result.delete(0, END)
def button_subtract():
first_number = result.get()
global f_num
global math
math = "subtraction"
f_num = int(first_number)
result.delete(0, END)
def button_multiply():
first_number = result.get()
global f_num
global math
math = "multiplication"
f_num = int(first_number)
result.delete(0, END)
def button_divide():
first_number = result.get()
global f_num
global math
math = "division"
f_num = int(first_number)
result.delete(0, END)
def button_equals():
second_number = result.get()
result.delete(0, END)
if math == "addition":
result.insert(0, f_num + int(second_number))
if math == "subtraction":
result.insert(0, f_num - int(second_number))
if math == "multiplication":
result.insert(0, f_num * int(second_number))
if math == "division":
result.insert(0, f_num / int(second_number))
button_1 = Button(window, text="1", padx=40, pady=20, command=lambda: button_click(1))
button_2 = Button(window, text="2", padx=40, pady=20, command=lambda: button_click(2))
button_3 = Button(window, text="3", padx=40, pady=20, command=lambda: button_click(3))
button_4 = Button(window, text="4", padx=40, pady=20, command=lambda: button_click(4))
button_5 = Button(window, text="5", padx=40, pady=20, command=lambda: button_click(5))
button_6 = Button(window, text="6", padx=40, pady=20, command=lambda: button_click(6))
button_7 = Button(window, text="7", padx=40, pady=20, command=lambda: button_click(7))
button_8 = Button(window, text="8", padx=40, pady=20, command=lambda: button_click(8))
button_9 = Button(window, text="9", padx=40, pady=20, command=lambda: button_click(9))
button_0 = Button(window, text="0", padx=40, pady=20, command=lambda: button_click(0))
button_add = Button(window, text="+", padx=39, pady=20, command=button_add)
button_subtract = Button(window, text="-", padx=41, pady=20, command=button_subtract)
button_multiply = Button(window, text="*", padx=40, pady=20, command=button_multiply)
button_divide = Button(window, text="/", padx=41, pady=20, command=button_divide)
button_clear = Button(window, text="清空", padx=79, pady=20, command=button_clear)
button_equals = Button(window, text="=", padx=91, pady=20, command=button_equals)
button_1.grid(row=3, column=0)
button_2.grid(row=3, column=1)
button_3.grid(row=3, column=2)
button_4.grid(row=2, column=0)
button_5.grid(row=2, column=1)
button_6.grid(row=2, column=2)
button_7.grid(row=1, column=0)
button_8.grid(row=1, column=1)
button_9.grid(row=1, column=2)
button_0.grid(row=4, column=0)
button_clear.grid(row=4, column=1, columnspan=2)
button_add.grid(row=5, column=0)
button_subtract.grid(row=6, column=0)
button_multiply.grid(row=6, column=1)
button_divide.grid(row=6, column=2)
button_equals.grid(row=5, column=1, columnspan=2)
window.mainloop()
06 | ?? 歸納總結(jié):代碼重構(gòu) \color{cyan}{歸納總結(jié):代碼重構(gòu)} 歸納總結(jié):代碼重構(gòu)??
通過前面所學(xué)的函數(shù)、類和對(duì)象的知識(shí),把上面的草稿版計(jì)算器重構(gòu)一下,順便換一下這個(gè)丑陋的界面了??????
import tkinter as tk
class Calculator:
def __init__(self, master):
self.master = master
master.title('Calculator')
self.create_widgets()
def create_widgets(self):
self.display = tk.Entry(self.master, width=30, justify='right', font=('Arial', 16))
self.display.grid(row=0, column=0, columnspan=4, padx=10, pady=10)
# Add Buttons
self.button_1 = tk.Button(self.master, text='1', width=7, height=2, command=lambda: self.button_click('1'))
self.button_2 = tk.Button(self.master, text='2', width=7, height=2, command=lambda: self.button_click('2'))
self.button_3 = tk.Button(self.master, text='3', width=7, height=2, command=lambda: self.button_click('3'))
self.button_4 = tk.Button(self.master, text='4', width=7, height=2, command=lambda: self.button_click('4'))
self.button_5 = tk.Button(self.master, text='5', width=7, height=2, command=lambda: self.button_click('5'))
self.button_6 = tk.Button(self.master, text='6', width=7, height=2, command=lambda: self.button_click('6'))
self.button_7 = tk.Button(self.master, text='7', width=7, height=2, command=lambda: self.button_click('7'))
self.button_8 = tk.Button(self.master, text='8', width=7, height=2, command=lambda: self.button_click('8'))
self.button_9 = tk.Button(self.master, text='9', width=7, height=2, command=lambda: self.button_click('9'))
self.button_0 = tk.Button(self.master, text='0', width=7, height=2, command=lambda: self.button_click('0'))
# Layout Buttons
self.button_1.grid(row=1, column=0, padx=10, pady=10)
self.button_2.grid(row=1, column=1, padx=10, pady=10)
self.button_3.grid(row=1, column=2, padx=10, pady=10)
self.button_4.grid(row=2, column=0, padx=10, pady=10)
self.button_5.grid(row=2, column=1, padx=10, pady=10)
self.button_6.grid(row=2, column=2, padx=10, pady=10)
self.button_7.grid(row=3, column=0, padx=10, pady=10)
self.button_8.grid(row=3, column=1, padx=10, pady=10)
self.button_9.grid(row=3, column=2, padx=10, pady=10)
self.button_0.grid(row=4, column=1, padx=10, pady=10)
# Operator Buttons
self.button_add = tk.Button(self.master, text='+', width=7, height=2, command=lambda: self.button_click('+'))
self.button_subtract = tk.Button(self.master, text='-', width=7, height=2, command=lambda: self.button_click('-'))
self.button_multiply = tk.Button(self.master, text='*', width=7, height=2, command=lambda: self.button_click('*'))
self.button_divide = tk.Button(self.master, text='/', width=7, height=2, command=lambda: self.button_click('/'))
self.button_equal = tk.Button(self.master, text='=', width=7, height=2, command=self.calculate)
self.button_clear = tk.Button(self.master, text='Clear', width=7, height=2, command=self.clear)
# Layout Operator Buttons
self.button_add.grid(row=1, column=3, padx=10, pady=10)
self.button_subtract.grid(row=2, column=3, padx=10, pady=10)
self.button_multiply.grid(row=3, column=3, padx=10, pady=10)
self.button_divide.grid(row=4, column=3, padx=10, pady=10)
self.button_equal.grid(row=4, column=2, padx=10, pady=10)
self.button_clear.grid(row=4, column=0, padx=10, pady=10)
def button_click(self, number):
current = self.display.get()
self.display.delete(0, tk.END)
self.display.insert(0, str(current) + str(number))
def clear(self):
self.display.delete(0, tk.END)
def calculate(self):
try:
value = eval(self.display.get())
self.clear()
self.display.insert(0, value)
except:
self.clear()
self.display.insert(0, 'Error')
root = tk.Tk()
app = Calculator(root)
root.mainloop()
07 | ?? 寫在最后 \color{purple}{寫在最后} 寫在最后??
學(xué)習(xí)Python GUI編程可以掌握基本的圖形界面設(shè)計(jì)技能,能夠開發(fā)出各種有趣的應(yīng)用程序,比如桌面應(yīng)用程序、游戲、工具等等,以下是今天學(xué)習(xí)GUI編程的一些總結(jié):
-
學(xué)習(xí)Python的GUI編程,需要先掌握Python的基礎(chǔ)語法和函數(shù)庫的使用。
-
Tkinter是Python的標(biāo)準(zhǔn)GUI庫,有很多教程和文檔可以學(xué)習(xí)。如果希望做一些復(fù)雜的交互,可以使用第三方庫PyQt或wxPython。
-
學(xué)習(xí)GUI編程的核心技能在于掌握各種組件的使用和布局方式的選擇。常見的組件包括按鈕、標(biāo)簽、文本框、列表框、下拉框等等。
-
學(xué)習(xí)GUI編程需要多寫代碼多實(shí)踐,可以參考各種開源項(xiàng)目,也可以自己動(dòng)手實(shí)現(xiàn)一些小型應(yīng)用。
-
GUI編程中需要非常注重用戶體驗(yàn)和界面設(shè)計(jì),需要考慮不同分辨率、不同操作系統(tǒng)、不同用戶的使用習(xí)慣等等因素。文章來源:http://www.zghlxwxcb.cn/news/detail-447617.html
總之,學(xué)習(xí)Python的GUI編程可以幫助我們掌握很多有用的開發(fā)技能,也可以讓應(yīng)用程序擁有更好的界面和交互體驗(yàn)。
最后希望大家能一起學(xué)習(xí),加油進(jìn)步,也希望大佬們能看下我的代碼還有哪里可以繼續(xù)改進(jìn)的地方。
如果本篇文章對(duì)你有所幫助的話,請給我一個(gè)三連支持哦,謝謝各位大佬的觀看,下期再見啦了ヾ(≧▽≦*)o文章來源地址http://www.zghlxwxcb.cn/news/detail-447617.html
到了這里,關(guān)于Python進(jìn)階知識(shí)(2)—— 什么是GUI編程?一起來學(xué)習(xí)用Python,Tkinter“做畫”吧的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!