我是新手,正在慢慢嘗試學習一些 tkinter,我為基本專案找到了一些不錯的資源,但是對代碼的解釋有點缺乏,我希望有人能幫助我理解一些什么我已經一起報廢了。
完整代碼
from tkinter import *
#Create window and give it title
root = Tk()
root.title('Tip Calculator - Charlie')
#define the window sizes
window_width = 600
window_height = 400
#get the screen size
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
#solve for center of screen based on window & screen Size
center_x = int(screen_width/2 - window_width/2)
center_y = int(screen_height/2 - window_height/2)
#implement the above to position window width/height
root.geometry(f'{window_width}x{window_height} {center_x} {center_y}')
var = IntVar()
frame = LabelFrame(root,text = "Tip %")
frame.pack(pady=10)
r1 = Radiobutton(frame, text = 'no Tip', variable = var, value = 0)
r1.grid(column =0, row =1)
r2= Radiobutton(frame, text = '10 %', variable = var, value = 1)
r2.grid(column=0, row =2)
r3=Radiobutton(frame, text = '15 %', variable = var, value = 2)
r3.grid(column=1, row = 1)
r4= Radiobutton(frame, text = '20 %', variable = var, value =3)
r4.grid(column=1, row=2)
有問題的代碼
var = IntVar()
frame = LabelFrame(root,text = "Tip %")
frame.pack(pady=10)
r1 = Radiobutton(frame, text = 'no Tip', variable = var, value = 0)
r1.grid(column =4, row =1)
r2= Radiobutton(frame, text = '%', variable = var, value = 1)
r2.grid(column=4, row =2)
所以我在一定程度上理解 r1 是“單選按鈕”的變數名稱,文本設定為按鈕旁邊彈出的內容。問題是 variable = var, value = 1 和前 3 行我不知所措為什么要創建一個名為 var 的變數以及設定它 = IntVar() 有什么作用?
框架是小部件嗎?我假設 pack 是另一個版本的定位(網格)?
uj5u.com熱心網友回復:
variable = var表示當單選按鈕被選中時,IntVar變數中的var應該被賦予這個單選按鈕的值。并且該value選項指定應該用于每個單選按鈕的值。
因此,在您選擇no Tip單選按鈕后,var.get()將0. 選擇%按鈕后,var.get()將1. 這就是您如何找出他們使用單選按鈕做出的選擇。
稍后在代碼中您可以撰寫:
if var.get() == 0:
tip = 0
elif var.get() == 1:
tip = 0.10 * total_price
elif var.get() == 2:
tip = 0.15 * total_price
elif var.get() == 3:
tip = 0.20 * total_price
price_with_tip = total_price tip
pack()是幾何管理器,是 和 的替代grid()品place()。看這個教程
uj5u.com熱心網友回復:
為了回答您的后續問題,我認為這將幫助您了解如何處理后續步驟。
from tkinter import *
def get_value():
tab = 50 # set to 50 but could get a value from entry if you want
choice = var.get()
print(choice) # print your choice for clarification
ten_percent = tab * .1 # math for tip
total = ten_percent tab
if choice == 1:
print("Your total plus tip is:", total)
print("Your tip amount was:", ten_percent)
#Create window and give it title
root = Tk()
root.title('Tip Calculator - Charlie')
#define the window sizes
window_width = 600
window_height = 400
#get the screen size
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
#solve for center of screen based on window & screen Size
center_x = int(screen_width/2 - window_width/2)
center_y = int(screen_height/2 - window_height/2)
#implement the above to position window width/height
root.geometry(f'{window_width}x{window_height} {center_x} {center_y}')
var = IntVar()
frame = LabelFrame(root,text = "Tip %")
frame.pack(pady=10)
r1 = Radiobutton(frame, text = 'no Tip', variable = var, value = 0)
r1.grid(column =0, row =1)
r2= Radiobutton(frame, text = '10 %', variable = var, value = 1)
r2.grid(column=0, row =2)
r3=Radiobutton(frame, text = '15 %', variable = var, value = 2)
r3.grid(column=1, row = 1)
r4= Radiobutton(frame, text = '20 %', variable = var, value =3)
r4.grid(column=1, row=2)
button = Button(root, text="Check", command= get_value)
button.pack()
uj5u.com熱心網友回復:
這個頁面會給你一個很好的解釋電臺小部件
Var 用于建立整數變數,而不是字串變數。(IntVar() 與 StringVar())。通讀該檔案中的解釋,讓我知道它是否仍然沒有意義。至于您對 pack() 的問題,它只是一種快速方法,如果您不指定其他任何內容,它實際上會將小部件包裝在中心和頂部。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/461131.html
上一篇:這個gtk小部件叫什么
