我有幾個具有不同值的單選按鈕,并且對一個變數很緊。即使變數在選擇時應該采用按鈕的值,但它不會。這是我正在使用的代碼。
mimeType = StringVar()
wave = StringVar(value="audio/wav")
mp3 = StringVar(value='audio/mp3')
mp4 = StringVar(value='audio/mp4')
mp2 = StringVar(value='audio/mp2')
flac = StringVar(value='audio/flac')
m4a = StringVar(value='audio/m4a')
MP3 = Radiobutton(window, text = "MP3 Format", variable = mimeType,
height=5,
width = 20, value=mp3,bg = "#36DEE5", activebackground= "#36DEE5",
MP4 = Radiobutton(window, text = "MP4 Format", variable = mimeType,
height=5,
width = 20,value=mp4,bg = "#36DEE5", activebackground= "#36DEE5")
MP2 = Radiobutton(window, text = "MP2 Format", variable = mimeType,
height=5,
width = 20,value=mp2,bg = "#36DEE5", activebackground= "#36DEE5")
WAV = Radiobutton(window, text = "WAV Format", variable = mimeType,
height=5,
width = 20, value=wave,bg = "#36DEE5", activebackground= "#36DEE5")
M4A = Radiobutton(window, text = "M4A Format", variable = mimeType,
height=5,
width = 20,value=m4a,bg = "#36DEE5", activebackground= "#36DEE5",
print(mimeType.get()) # This doesn't return anything either
特別提示: print(mimeType.get())不回傳任何東西!
所以我想要的是,變數mimeType在選擇時應該采用 Radio Buttons 的值。
uj5u.com熱心網友回復:
該value引數不采用IntVar()或StringVar()物件。您可以直接為其分配字串或整數。
print(mimeType.get())然后將列印value存盤在mimeType.
第二個問題是您的列印陳述句甚至在用戶單擊單選按鈕之前就已執行。
上述問題有兩種解決方案。
- 創建一個按鈕,在用戶單擊按鈕時顯示所選值。
command當用戶單擊單選按鈕時,使用引數呼叫函式(顯示所選值)。
第二種解決方案的示例代碼:
MP4 = Radiobutton(window, text = "MP4 Format", variable = mimeType, height=5, width = 20,value = 'audio/mp4', bg = "#36DEE5", activebackground= "#36DEE5", command = lambda : print(mimeType.get()))
添加command = lambda : print(mimeType.get())到所有單選按鈕以value在用戶單擊單選按鈕后立即列印出來。
uj5u.com熱心網友回復:
以下是一個可運行的答案。
from tkinter import *
window = Tk()
mimeType = StringVar() # is a string variable but in the original code mimeType was being set an object.
mimeType.set('Initialize') # So the later print statement will print something
# original code value = was being set to objects.
wave = StringVar(value="audio/wav") # These are objects no longer needed?
mp3 = StringVar(value='audio/mp3') # These are objects no longer needed?
mp4 = StringVar(value='audio/mp4') # These are objects no longer needed?
mp2 = StringVar(value='audio/mp2') # These are objects no longer needed?
flac = StringVar(value='audio/flac') # These are objects no longer needed?
m4a = StringVar(value='audio/m4a') # These are objects no longer needed?
def display_current_mimetype():
print(mimeType.get())
# Remove assignment of Radiobutton from original code unless the created
# objects are needed later.
# Modify code to value = string not value = object
Radiobutton(window, text="MP3 Format", variable=mimeType,
height=5,
width=20, value='audio/mp3', bg="#36DEE5",
activebackground="#36DEE5", command=display_current_mimetype).pack(
anchor=W)
Radiobutton(window, text="MP4 Format", variable=mimeType,
height=5,
width=20, value='audio/mp4', bg="#36DEE5",
activebackground="#36DEE5", command=display_current_mimetype).pack(
anchor=W)
Radiobutton(window, text="MP2 Format", variable=mimeType,
height=5,
width=20, value='audio/mp2', bg="#36DEE5",
activebackground="#36DEE5", command=display_current_mimetype).pack(
anchor=W)
Radiobutton(window, text="WAV Format", variable=mimeType,
height=5,
width=20, value="audio/wav", bg="#36DEE5",
activebackground="#36DEE5", command=display_current_mimetype).pack(
anchor=W)
Radiobutton(window, text="M4A Format", variable=mimeType,
height=5,
width=20, value='audio/m4a', bg="#36DEE5",
activebackground="#36DEE5", command=display_current_mimetype).pack(
anchor=W)
print(mimeType.get()) # Will print the initial value
mainloop()
uj5u.com熱心網友回復:
我認為您設定的Radiobuttons 不正確。將它們的選項設定為 StringVar 是不正確的,您應該將其設定為您希望在選擇時var=分配給的值。mimeType StringVarRadiobutton
這是我的意思的一個可運行的示例。注意我已經重命名了你的幾個變數并重新格式化了代碼,以更緊密地遵循![將單選按鈕的輸入獲取到變數 [Tkinter]](https://img.uj5u.com/2022/04/01/5da590f74e2948fca15fb9594a83ac3b.png)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/453972.html
