天天看點

簡單 Python 快樂之旅之:Python 基礎文法之 GUI 專題

文章目錄

  • ​​1. Python tkinter 設定特定或固定的視窗大小​​
  • ​​1.1. 示例一:在 Python tkinter 中設定視窗大小​​
  • ​​1.2. 設定視窗大小​​
  • ​​1.3. 小結​​
  • ​​2. Python tkinter 按鈕例子​​
  • ​​2.1. Python tkinter 按鈕​​
  • ​​2.2. 示例一:使用 tkinter 庫的按鈕​​
  • ​​2.3. 小結​​
  • ​​3. 按鈕點選調用函數​​
  • ​​3.1. 按鈕點選事件調函數​​
  • ​​4. 更改字型系列、字型大小及格式​​
  • ​​4.1. 改變 tkinter 按鈕的字型系列​​
  • ​​4.2. 改變 tkinter 按鈕的字型大小​​
  • ​​4.3. 改變 tkinter 按鈕的字型粗細​​
  • ​​4.4. 改變 tkinter 按鈕的字型系列、字型大小以及字型粗細。​​
  • ​​4.5. 小結​​

1. Python tkinter 設定特定或固定的視窗大小

在使用 Python tkinter 給視窗設定一個特定大小時,使用 Tk() 類變量的 geometry() 函數。

from tkinter import *
gui = Tk()
gui.geometry("widthxheight")      

其中,width 和 height 應該分别由表示視窗寬度和高度的整型數字所代替。留意傳遞給 geometry() 的 width 和 height 變量之間有一個 x。

注意:請注意視窗大小 width x height 并不包含視窗标題部分。

1.1. 示例一:在 Python tkinter 中設定視窗大小

在本示例中,我們将使用 geometry() 方法來給 Tk() 視窗設定一個固定的 500 乘 200 的大小。

from tkinter import *
gui = Tk(className='Python Examples - Window Size')
# set window size
gui.geometry("500x200")
gui.mainloop()      

執行和輸出:

簡單 Python 快樂之旅之:Python 基礎文法之 GUI 專題

可以看到,在執行改程式的時候,将會打開一個由 geometry() 函數中定義視窗大小的 GUI 視窗。

1.2. 設定視窗大小

現在我們來改變一下提供給 geometry() 函數的寬度和長度,比如 300 乘 300。

from tkinter import *
gui = Tk(className='Python Examples - Window Size')
# set window size
gui.geometry("300x300")
gui.mainloop()      

執行和輸出:

簡單 Python 快樂之旅之:Python 基礎文法之 GUI 專題

1.3. 小結

本節我們通過詳盡的示例了解到如何給基于 tkinter 的 GUI 程式設定視窗大小。

2. Python tkinter 按鈕例子

2.1. Python tkinter 按鈕

我們将使用 Python 的 tkinter 庫來實作 Python GUI 裡的按鈕。

添加一個按鈕到 tkinter 視窗的文法如下:

mybutton = Button(master, option=value)
mybutton.pack()      

其中 master 是為你要添加到的那個視窗。tkinter 庫為按鈕構造子提供了不同的選項以更改其外觀。

選項
text 按鈕标簽的文本顯示
width 設定按鈕的寬度
height 設定按鈕的高度
bg 設定按鈕的背景色
fg 設定按鈕标簽的字型顔色
activebackground 設定按鈕被點選後的背景色
activeforeground 設定按鈕被點選後的按鈕标簽的字型顔色
command 當按鈕被點選時要調用的函數
font 設定按鈕标簽的字型大小、格式
image 設定按鈕圖檔

2.2. 示例一:使用 tkinter 庫的按鈕

在接下來的示例中,我們将建立一個 tkinter 按鈕。

from tkinter import *
gui = Tk(className='Python Examples - Button')
gui.geometry("500x200")
# create button
button = Button(gui, text='My Button', width=40, height=3, bg='#0052cc', fg='#ffffff', activebackground='#0052cc', activeforeground='#aaffaa')
# add button to gui window
button.pack()
gui.mainloop()      

執行和輸出:

簡單 Python 快樂之旅之:Python 基礎文法之 GUI 專題

2.3. 小結

本節我們了解到了如何使用 tkinter 庫來建立一個 GUI 按鈕。

3. 按鈕點選調用函數

當一個 tkinter 按鈕被點選的時候,你可以使用 command 屬性來調用一個函數。将你希望按鈕被點選時要調用到的函數名指派給 command 屬性即可。

以下是按鈕被點選時調用一個函數的僞代碼:

def someFunction:
    function body

tkWindow = Tk()

button = Button(tkWindow, command=someFunction)      

或者你也可以在定義該按鈕之後指派 command:

def someFunction:
    function body

tkWindow = Tk()

button = Button(tkWindow)
button['command'] =      

3.1. 按鈕點選事件調函數

from tkinter import *
from tkinter import messagebox

tkWindow = Tk()
tkWindow.geometry('400x150')
tkWindow.title('Defonds.net Tkinter Example')
def showMsg():
    messagebox.showinfo('Message', 'You clicked the Submit button!')

button = Button(tkWindow, text='Submit', command=showMsg)
button.pack()

tkWindow.mainloop()      

執行和輸出:

簡單 Python 快樂之旅之:Python 基礎文法之 GUI 專題

在提供使用者點選按鈕後調用函數時需要注意:

  • 在按鈕的定以前定義該函數
  • command 選項的值是不帶任何引号的函數名

4. 更改字型系列、字型大小及格式

要更改字型屬性,如字型系列、字型大小、字型粗細等,可以使用 tkinter.font 包。

在程式中,引入 tkinter.font as font 并提供變量給 font.Font()。

4.1. 改變 tkinter 按鈕的字型系列

以下示例中,我們将通過使用 family 命名參數給 font.Font() 來改變 tkinter 按鈕的字型系列。

from tkinter import *
import tkinter.font as font

gui = Tk(className='Python examples - Button')
gui.geometry('500x200')

# define font
myFont = font.Font(family='Helvetica')

# create button
button = Button(gui, text='My Button', bg='#0052cc', fg='#ffffff')
# apply font to the button label
button['font'] = myFont
# add button to gui window
button.pack()

gui.mainloop()      

執行和輸出:

簡單 Python 快樂之旅之:Python 基礎文法之 GUI 專題

如果沒有提供 font,該按鈕看起來會是這個樣子:

簡單 Python 快樂之旅之:Python 基礎文法之 GUI 專題

4.2. 改變 tkinter 按鈕的字型大小

我們還可以通過傳遞命名參數 text 給 font.Font() 來改變 tkinter 按鈕中文本的字型大小。

from tkinter import *
import tkinter.font as font

gui = Tk(className='Python examples - Button')
gui.geometry('500x200')

# define font
myFont = font.Font(size=30)

# create button
button = Button(gui, text='My Button', bg='#0052cc', fg='#ffffff')
# apply font to the button label
button['font'] = myFont
# add button to gui window
button.pack()

gui.mainloop()      

執行和輸出:

簡單 Python 快樂之旅之:Python 基礎文法之 GUI 專題

4.3. 改變 tkinter 按鈕的字型粗細

我們還可以通過傳遞命名參數 weight 給 font.Font() 來改變 tkinter 按鈕中文本的字型粗細。

from tkinter import *
import tkinter.font as font

gui = Tk(className='Python examples - Button')
gui.geometry('500x200')

# define font
myFont = font.Font(weight='bold')

# create button
button = Button(gui, text='My Button', bg='#0052cc', fg='#ffffff')
# apply font to the button label
button['font']=myFont
# add button to gui window
button.pack()

gui.mainloop()      

執行和輸出:

簡單 Python 快樂之旅之:Python 基礎文法之 GUI 專題

4.4. 改變 tkinter 按鈕的字型系列、字型大小以及字型粗細。

我們可以把以上設定打包一起傳給 font.Font()。

from tkinter import *
import tkinter.font as font

gui = Tk(className='Python examples = Button')
gui.geometry('500x200')

# define font
myFont = font.Font(family='Helvetica', size=20, weight='bold')

# create button
button = Button(gui, text='My Button', bg='#0052cc', fg='#ffffff')
# apply font to the button label
button['font']=myFont
# add button to gui window
button.pack()

gui.mainloop()      

執行和輸出:

簡單 Python 快樂之旅之:Python 基礎文法之 GUI 專題

我們将字型系列改為 Courier 然後重新運作該程式:

myFont = font.Font(family='Courier', size=20, weight='bold')      

執行和輸出:

簡單 Python 快樂之旅之:Python 基礎文法之 GUI 專題

4.5. 小結

  • ​​Python tkinter Set Specific or Fixed Window Size​​
  • ​​Python Button Example​​
  • ​​Python tkinter – Call a Function on Clicking Button​​
  • ​​Python tkinter Button – Change Font Style​​