python - Tkinter 单选按钮 动态选择
问题描述
Option-1我分别创建了一个带有 2 个单选按钮的简单程序Option-2。
默认情况下,Option-1选中并"You selected Option 1"
显示标签。
在运行时,如果用户选择,则应显示Option-2标签。"You selected Option 2"
有了这个要求,我创建了下面的程序。但不工作。Option-2选择单选按钮后没有响应。
我的代码如下。
你们中的任何人都可以解决我的问题吗?
import tkinter as tk
class window1:
def __init__(self, master):
self.master = master
self.frame1 = tk.Frame(self.master, width=300, height=100)
self.frame1.place(x=5, y=5)
window1.priority = tk.IntVar(value=1)
def message_radio(self):
if self.priority.get() == 1:
Message1 = tk.Label(self.frame1, text="You selected Option 1").place(x=10, y=35)
else:
Message2 = tk.Label(self.frame1, text="You selected Option 2").place(x=10, y=35)
def radio_button(self):
radiobutton1 = tk.Radiobutton(self.frame1, text="Option 1", variable=self.priority,
value=1,command=window1.message_radio(self))
radiobutton1.place(x=10, y=10)
radiobutton2 = tk.Radiobutton(self.frame1, text="Option 2", variable=self.priority,
value=2,command=window1.message_radio(self))
radiobutton2.place(x=175, y=10) def main():
def main():
root = tk.Tk()
root.geometry( "300x100" )
app = window1( root )
app.radio_button()
root.mainloop()
if __name__ == '__main__':
main()
解决方案
推荐阅读
- react-redux - 过滤 redux 中 i 状态 id 被删除的更新值,并显示那些状态为 ative 的值
- docker - Docker Redis - 播种数据 - 无法通过 Dockerfile 或 shell 脚本执行 redis-cli
- cucumber - 如何使用宁静黄瓜运行所有功能文件
- javascript - 点击链接展开 UID/PW 字段
- python - 如何从 robotsframework 中获取 Testsuite、Testcase 名称和单个测试内容
- reactjs - 使用 UseEffect 从 API 获取数据
- json - 查询嵌套 JSON 中的项目
- javascript - 如何使用 Lerna 管理多个包的多个版本?
- sql - 如何通过所有员工姓名明智地获得每个部门的最高薪水
- php - Jwt 令牌不验证 Rest Api Php