gpt4 book ai didi

python - 将单选按钮的输入获取到变量 [Tkinter]

转载 作者:行者123 更新时间:2023-12-01 23:01:46 37 4
gpt4 key购买 nike

我有几个单选按钮,它们具有不同的值,并且对一个变量很严格。尽管该变量在被选中时应该采用按钮的值,但事实并非如此。这是我正在使用的代码。

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 应该在选中时采用单选按钮的值。

最佳答案

value 参数不采用 IntVar()StringVar() 对象。您可以直接为其分配一个字符串或整数。

print(mimeType.get()) 将打印存储在 mimeType 中的 value

第二个问题是您的打印语句甚至在用户单击单选按钮之前就已执行。

上述问题有两种解决方法。

  1. 创建一个按钮,当用户点击该按钮时显示所选值。
  2. 当用户点击单选按钮时,使用 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

关于python - 将单选按钮的输入获取到变量 [Tkinter],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71695487/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com