tkinter多个单选按钮

时间:2018-11-11 22:54:02

标签: python-3.x tkinter

我有2组单选按钮可供选择,如果我选择了第二组中的一个,则尽管相关功能仍在起作用,但第一组中的所选按钮将被取消选择。 这是示例和代码

对于“是否需要特定号码?”(收音机1和收音机2)和“是否要发送具有不同电子邮件地址的电子邮件?”(收音机5和收音机6),我只能选择其中之一。例如,如果我选择收音机1,则选择收音机5或收音机6,则取消选择收音机1。我不确定我做错了什么。 您能指出正确的方法吗? 这是我的代码

from tkinter import *
from tkinter import messagebox
from tkinter.ttk import *
root = Tk()
def guiUDP():
    def enableSPNum() :
        entry5.configure(state = 'normal')
    def disableSPNum() :
        entry5.configure(state = 'disabled')
    def diffEmail() :
        entry8.configure(state = 'normal')
    def defaultEmail() :
        entry8.configure(state = 'disabled')
    def submitReq() :        
        userID = entry1.get()
        siteCode = entry2.get().upper()
        firstName = entry3.get().title()
        lastName = entry4.get().upper()
        spNum = entry5.get()
        vmEnable = select1.get()
        agentID = entry7.get().lower()
        eMail = entry8.get()
    entry1 = Entry(root)
    entry1.grid(column = 1, row = 0)
    label1 = Label(root, text = 'userID : ')
    label1.grid(column = 0, row = 0, sticky = E)

    entry2 = Entry(root)
    entry2.grid(column = 1, row = 1)
    label2 = Label(root, text = 'Site Code :')
    label2.grid(column = 0, row = 1, sticky = E)

    entry3 = Entry(root)
    entry3.grid(column = 1, row = 2)
    label3 = Label(root, text = 'First Name :')
    label3.grid(column = 0, row = 2, sticky = E)

    entry4 = Entry(root)
    entry4.grid(column = 1, row = 3)
    label4 = Label(root, text = 'Last Name :')
    label4.grid(column = 0, row = 3, sticky = E)

    #selected = IntVar()
    label5 = Label(root, text = 'Need Specific Number? :')
    label5.grid(column = 0, row = 4, sticky = E)

    rad1 = Radiobutton(root,text='Yes', value = 1, command = enableSPNum)
    rad2 = Radiobutton(root,text='No (Default)', value = 2, command = disableSPNum)
    rad1.grid(column = 1, row = 4)
    rad2.grid(column = 2, row = 4)

    entry5 = Entry(root, state = 'disabled')
    entry5.grid(column = 1, row = 6)
    label6 = Label(root, text = 'Extension :')
    label6.grid(column = 0, row = 6, sticky = E)

    label7 = Label(root, text = 'Voicemail Required? :')
    label7.grid(column = 0, row = 7, sticky = E)

    select1 = IntVar()
    rad3 = Radiobutton(root,text='Yes', value = 3, variable = select1)
    rad4 = Radiobutton(root,text='No', value = 4, variable=select1)
    rad3.grid(column = 1, row = 7)
    rad4.grid(column = 2, row = 7)

    entry7 = Entry(root)
    entry7.grid(column = 1, row = 8)
    label8 = Label(root, text = 'Your userID :')
    label8.grid(column = 0, row = 8, sticky = E)

    label9 = Label(root, text = 'Want to send email with differnt email adrress? :')
    label9.grid(column = 0, row = 9, sticky = E)

    #selected = IntVar()
    rad5 = Radiobutton(root,text='Yes', value = 5, command = diffEmail)
    rad6 = Radiobutton(root,text='No', value = 6, command = defaultEmail)
    rad5.grid(column = 1, row = 9)
    rad6.grid(column = 2, row = 9)

    entry8 = Entry(root, state = 'disabled')
    entry8.grid(column = 1, row = 10)
    label10 = Label(root, text = 'Full Email Address :')
    label10.grid(column = 0, row = 10, sticky = E)

    btn = Button(root, text = 'Submit', command = submitReq)
    btn.grid(column = 2, row = 20)



mainMenu = Menu(root)
root.configure(menu=mainMenu)
guiMenu = Menu(mainMenu, tearoff = 0)
mainMenu.add_cascade(label = 'TEST', menu=guiMenu)
guiMenu.add_command(label = 'TEST', command = guiUDP)
root.mainloop()
root.geometry('600x600')

1 个答案:

答案 0 :(得分:0)

您尚未为第一和第三行单选按钮分配变量。如果我询问不带变量的单选按钮,它们会说它们绑定到selectedButton,这对所有按钮都是相同的。我猜这是缺少变量时的某种默认行为。如果为关联的单选按钮的每一行创建一个变量并将其分配给它们,效果就会消失。