好的,所以今天的問題是我決定撰寫一個代碼來練習 Python 的一系列技能。所以代碼將是轉換的 Tkinter 版本。每次潛在的轉換最終都會有一個組合框;希望在最少的幫助下,我會用幾個問題來解決大部分問題,將我重定向到解決方案!
所以目前我有 24 行在其中創建了根視窗。視窗的大小應完全取決于螢屏尺寸(第 7 行)。當前只有 1 項列出“長度”的組合框。
因此,這是許多/大多數可見代碼都會出現問題的地方。第 16-22 行是我試圖弄清楚如何寫這個的嘗試。(我可能需要一個比 combo_0 更好的名稱,但我的時間不夠用了.
from tkinter import *
from tkinter.ttk import *
#root window
root = Tk()
root.title("Conversion")
root.geometry(f'600x400 {int(root.winfo_screenwidth()/2-600/2)} {int(root.winfo_screenheight()/2-400/2)}')
#drop down widget "combo"
combo = Combobox(root)
combo['values']= ("Length")
combo.current(0) #set the selected item 'default'
combo.grid(column=0, row=0)
#widget check
if combo.get() == 0:
combo_0 = Combobox(root)
combo['values'] = ("Millimeter", "Centimeter", "meter"
"Kilometer", "Inch", "Foot", "Yard",
"Mile", "Nautical Mile")
combo_0.grid(column=1, row=1)
root.mainloop()
我要做的是基于當前的第一個Combobox,唯一的選擇是“長度”是在選擇它更改以具有適當的條目/組合框時。因此,例如,當“長度”時,它需要兩個具有相同選項的組合框(“毫米”、“厘米”、“米”、“公里”、“英寸”、“英尺”、“碼”、“英里”、“海里”)和一個輸入框。因此,您可以在相應的組合框中選擇英寸到英尺,然后輸入要轉換的數字。它還需要一種將結果回傳到螢屏的方法,以便用戶可以看到轉換。
uj5u.com熱心網友回復:
您正在尋找的是<<ComboboxSelected>>.
參考這里
以下是您將如何對其進行編碼:
.
.
#widget check
def displayNextComboxes(event):
if combo.get() == "Length":
comboValues = ("Millimeter", "Centimeter", "meter"
"Kilometer", "Inch", "Foot", "Yard",
"Mile", "Nautical Mile")
combo_0 = Combobox(root)
combo_0['values'] = comboValues
combo_0.grid(column=0, row=1)
Label(text="TO ").grid(column=1, row=1)
combo_1 = Combobox(root)
combo_1['values'] = comboValues
combo_1.grid(column=2, row=1)
userValue = StringVar()
valueEntry = Entry(root, textvariable = userValue)
valueEntry.grid(column=1, row=2)
convertButton = Button(root,text="Convert", command=Convert)
convertButton.grid(column=1, row=3)
#Binded the combobox's selection with the function `displayNextComboxes`:
combo.bind("<<ComboboxSelected>>", displayNextComboxes)
def Convert():
pass #write here the code to check,
#convert, and display the value entered by the user to convert.
root.mainloop()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/474316.html
