使用 tkinter 我希望能够使用相同的单选按钮从用户那里获得两个输入

Using tkinter I want to be able to get two inputs from the user using the same radiobuttons

使用此列表,我希望能够获得两个输入。这将存储在变量中。有什么建议么??请帮助我是 tkinter 的新手。

import tkinter as tk

class App():

    def __init__(self, master):

        self.master = master

        self.type_integration = None

        self.typeChoice = tk.StringVar()

        self.typeChoice.set(None) # This fixes the grayness of the radio buttons!

        self.typeFrame = tk.Frame(master)

        OPTIONS = [('Arsenal','Arsenal'),('Aston Villa','Aston Villa'),('Burnley','Burnley'),('Chelsea','Chelsea'),('Crystal Palace','Crystal Palace'),('Everton','Everton'),('Hull','Hull'),('Leicester','Leicester',),('Liverpool','Liverpool'),('Manchester City','Manchester City'),('Manchester United','Manchester United'),('Newcastle United','Newcastle United'),('Queens Park Rangers','Queens Park Rangers'),('Southampton','Southampton'),('Stoke','Stoke'),('Sunderland','Sunderland'),('Swansea','Swansea'),('Tottenham','Tottenham'),('West Bromwich Albion','West Bromwich Albion'), ('West Ham','West Ham')]

        for text, value in OPTIONS:

            tk.Radiobutton(self.typeFrame, text=text, variable=self.typeChoice, value=value).pack(anchor = 'w')

        tk.Button(self.typeFrame, text="Confirm Home Team", command=self.exit).pack(anchor = 'w')

        self.typeFrame.pack()

    def exit(self):

        self.type_integration = self.typeChoice.get()


        self.master.destroy()




    def getinput(self):

        return self.type_integration





master = tk.Tk()

app = App(master)

tk.mainloop()

home = app.getinput()
print(home)

我需要再做一个class吗??或者我可以重用代码吗??帮助将不胜感激。什么都愿意听,我不是很好

RadioButton 是一个用于实现 one-of-many 选择的 Tkinter 小部件。

你需要使用 CheckButton widget 来做你想做的事。

def __init__(self, master):
    ...
    self.variables = variables = []
    for text, value in OPTIONS:
        var = tk.StringVar()
        variables.append(var)
        tk.Checkbutton(self.typeFrame, text=text, variable=var,
                       onvalue=text, offvalue='').pack(anchor = 'w')
    ...

def exit(self):
    self.type_integration = ','.join(v.get() for v in self.variables if v.get())
    self.master.destroy()

更新

限制最多2个选择:

def __init__(self, master):
    ...
    self.variables = []
    self.checkboxs = []
    for text, value in OPTIONS:
        var = tk.StringVar()
        cb = tk.Checkbutton(self.typeFrame, text=text, variable=var,
                            onvalue=text, offvalue='', command=self.limit2)
        cb.pack(anchor = 'w')
        self.variables.append(var)
        self.checkboxs.append(cb)
    ...

def limit2(self):
    if sum(bool(v.get()) for v in self.variables) >= 2:
        # Disable non-selected items to limit selection
        for cb, v in zip(self.checkboxs, self.variables):
            if not v.get():
                cb['state'] = 'disabled'
    else:
        for cb in self.checkboxs:
            cb['state'] = 'normal'