将单选按钮的输入获取到变量 [Tkinter]
Getting Radio Button's Input to a Variable [Tkinter]
我有几个单选按钮,它们具有不同的值,并且只针对一个变量。尽管该变量在被选中时应该采用按钮的值,但事实并非如此。这是我正在使用的代码。
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())
什么都return!
所以我想要的是,变量 mimeType
应该在选中时采用单选按钮的值。
value
参数不接受 IntVar()
或 StringVar()
对象。你可以直接给它赋一个字符串或整数。
print(mimeType.get())
将打印存储在 mimeType
.
中的 value
第二个问题是您的打印语句甚至在用户单击单选按钮之前就已执行。
以上问题有两种解决方法。
- 创建一个按钮,当用户单击该按钮时显示所选值。
- 当用户单击单选按钮时,使用
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
。
以下是可运行的答案。
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()
我认为您设置的 Radiobutton
不正确。将他们的 var=
选项设置为 StringVar 是不正确的,您应该在选择 Radiobutton
时将其设置为要分配给 mimeType
StringVar
的值。
这是一个可运行的例子来说明我的意思。请注意,我已重命名您的几个变量并重新格式化代码以更严格地遵循 PEP 8 - Style Guide for Python Code 指南。
from tkinter import *
window = Tk()
MP3 = 'audio/mp3'
MP4 = 'audio/mp4'
MP2 = 'audio/mp2'
WAVE = 'audio/wav'
FLAC = 'audio/flac'
M4A = 'audio/m4a'
mimeType = StringVar(value='N/A')
BUTTON_OPTS = dict(height=1, width=20, bg='#36DEE5', activebackground= '#36DEE5',
variable=mimeType, command=lambda: print(mimeType.get()))
mp3_btn = Radiobutton(window, text='MP3 Format', value=MP3, **BUTTON_OPTS)
mp3_btn.pack()
mp4_btn = Radiobutton(window, text='MP4 Format', value=MP4, **BUTTON_OPTS)
mp4_btn.pack()
mp2_btn = Radiobutton(window, text='MP2 Format', value=MP2, **BUTTON_OPTS)
mp2_btn.pack()
wav_btn = Radiobutton(window, text='WAV Format', value=WAVE, **BUTTON_OPTS)
wav_btn.pack()
flac_btn = Radiobutton(window, text='FLAC Format', value=FLAC, **BUTTON_OPTS)
flac_btn.pack()
m4a_btn = Radiobutton(window, text='M4A Format', value=M4A, **BUTTON_OPTS)
m4a_btn.pack()
window.mainloop()
这是它的截图 运行:
我有几个单选按钮,它们具有不同的值,并且只针对一个变量。尽管该变量在被选中时应该采用按钮的值,但事实并非如此。这是我正在使用的代码。
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())
什么都return!
所以我想要的是,变量 mimeType
应该在选中时采用单选按钮的值。
value
参数不接受 IntVar()
或 StringVar()
对象。你可以直接给它赋一个字符串或整数。
print(mimeType.get())
将打印存储在 mimeType
.
value
第二个问题是您的打印语句甚至在用户单击单选按钮之前就已执行。
以上问题有两种解决方法。
- 创建一个按钮,当用户单击该按钮时显示所选值。
- 当用户单击单选按钮时,使用
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
。
以下是可运行的答案。
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()
我认为您设置的 Radiobutton
不正确。将他们的 var=
选项设置为 StringVar 是不正确的,您应该在选择 Radiobutton
时将其设置为要分配给 mimeType
StringVar
的值。
这是一个可运行的例子来说明我的意思。请注意,我已重命名您的几个变量并重新格式化代码以更严格地遵循 PEP 8 - Style Guide for Python Code 指南。
from tkinter import *
window = Tk()
MP3 = 'audio/mp3'
MP4 = 'audio/mp4'
MP2 = 'audio/mp2'
WAVE = 'audio/wav'
FLAC = 'audio/flac'
M4A = 'audio/m4a'
mimeType = StringVar(value='N/A')
BUTTON_OPTS = dict(height=1, width=20, bg='#36DEE5', activebackground= '#36DEE5',
variable=mimeType, command=lambda: print(mimeType.get()))
mp3_btn = Radiobutton(window, text='MP3 Format', value=MP3, **BUTTON_OPTS)
mp3_btn.pack()
mp4_btn = Radiobutton(window, text='MP4 Format', value=MP4, **BUTTON_OPTS)
mp4_btn.pack()
mp2_btn = Radiobutton(window, text='MP2 Format', value=MP2, **BUTTON_OPTS)
mp2_btn.pack()
wav_btn = Radiobutton(window, text='WAV Format', value=WAVE, **BUTTON_OPTS)
wav_btn.pack()
flac_btn = Radiobutton(window, text='FLAC Format', value=FLAC, **BUTTON_OPTS)
flac_btn.pack()
m4a_btn = Radiobutton(window, text='M4A Format', value=M4A, **BUTTON_OPTS)
m4a_btn.pack()
window.mainloop()
这是它的截图 运行: