首页 > 解决方案 > 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()

标签: pythontkinterradio-button

解决方案


推荐阅读