python tkinter单选按钮-在显示之前都选择了两个按钮

时间:2018-08-01 13:34:49

标签: python tkinter

我查看了所有可以找到的帖子,但似乎仍无法解决该问题。我有一个tkinter应用程序,正在尝试添加单选按钮。无论我尝试什么,都同时选择了两个按钮。我希望它们显示为空白。

请帮助我。我正在尝试弄清楚Python,并能够处理屏幕抓取,登录到试图锁定应用程序的站点,但无法直接了解此细节。您的代码很长,但我认为我要包括相关部分。问题区域是底部的最后一部分。

在此先感谢您提供的帮助。

创建用户名/密码标题区域

    self.heading_prompt = tk.Label(self.label_entry_frame, text='LOG `enter code here`on\      `ASSSISTANT')
    self.heading_prompt.config(fg='blue', font('times',heading_font, 'bold'))
    self.heading_prompt.pack(side = 'left')
    self.label_entry_frame.place(x = 112, y=100)

    #   User credentials - prompt
    self.user_name_prompt = tk.Label(self.name_entry_frame, text='Select `enter code here`User:                          ')
    self.user_name_prompt.config(fg='green', font=('times', ``regular_font, 'bold'))
    self.user_name_prompt.pack(side = 'left')

    #   Create Course Name Listbox
    self.user_name_selected = tk.Listbox(self.name_entry_frame,width =7, `enter code here`height = 2, highlightcolor="green",selectmode = 'single', `enter code here`exportselection = "False" )
    self.user_name_selected.config(fg='green', font=enter code here`enter code here`('times',regular_font, 'bold'))
    self.user_name_selected.insert(1, "Margy")
    self.user_name_selected.insert(2, "Steve")
    self.user_name_selected.pack(side = 'left')       
    self.name_entry_frame.place(x=10,y=130)

    #   Position Cursor into User Name Listbox
    self.user_name_selected.focus_set()


    #   User credentials - buttons
    self.user_no = 0
    #self.user_no.set(value = 0)
    user_rb_1 = tk.Radiobutton(self.name_entry_frame, text = "Margy", variable = self.user_no, value = 1)
    user_rb_1.config(fg='green', font=('times',regular_font, 'bold'))
    user_rb_1.pack(side = "left")
    user_rb_2 = tk.Radiobutton(self.name_entry_frame, text = "Steve", variable = self.user_no, value = 1)
    user_rb_2.config(fg='green', font=('times',regular_font, 'bold'))
    user_rb_2.pack(side = "left")
    self.name_entry_frame.place(x=30, y=160)

1 个答案:

答案 0 :(得分:2)

单选按钮的variable应该是tkinter variable

import tkinter as tk

root = tk.Tk()

user_no = tk.IntVar()

user_rb_1 = tk.Radiobutton(root, text = "Margy", variable = user_no, value = 1)
user_rb_1.pack()
user_rb_2 = tk.Radiobutton(root, text = "Steve", variable = user_no, value = 2)
user_rb_2.pack()

root.mainloop()

然后,您随时可以使用1获得选定的值(2user_no.get())。只要什么都没有选择,它将为0