如何使用 tkinter 调用相同的函数?

How do I call the same function with tkinter?

我有一个带有两个单选按钮的简单 tkinter window。如果我 select 'Yes',将添加一个新行和另外两个单选按钮。我每次单击 'Yes' 时都尝试使用相同的功能,但是如果将鼠标移到单选按钮上,它们会自动 selected。

这是我的代码:

from tkinter import *
import tkinter as tk

class Application(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)

        self.i = 1

        self.Frame1 = Frame(self)
        self.Frame1.grid(row=0, column=0)

        self.label = tk.Label(self.Frame1, text='Select Yes or No')
        self.label.grid(row=0, column=0)

        self.radioValueYesOrNo = tk.IntVar()
        self.RadiobuttonYesOrNo = tk.Radiobutton(self.Frame1, text='Yes',
                                                            variable=self.radioValueYesOrNo,
                                                            value=1,
                                                            command=self.addWidgets
                                                            )
        self.RadiobuttonYesOrNo.grid(row=0, column=1)
        
        self.RadiobuttonNonBoostPresent = tk.Radiobutton(self.Frame1, text='No',
                                                            variable=self.radioValueYesOrNo,
                                                            value=2)
        self.RadiobuttonNonBoostPresent.grid(row=0, column=2, padx=20)

    def addWidgets(self):
        valueRadioButton = self.returnValueRadiobutton()
        if valueRadioButton == 1:
            self.label = tk.Label(self.Frame1, text='Select Yes or No')
            self.label.grid(row=self.i, column=0)

            self.radioValueYesOrNo = tk.IntVar()
            self.RadiobuttonYesOrNo = tk.Radiobutton(self.Frame1, text='Yes',
                                                                variable=self.radioValueYesOrNo,
                                                                value=1,
                                                                command=self.addWidgets
                                                                )
            self.RadiobuttonYesOrNo.grid(row=self.i, column=1)
            self.RadiobuttonNonBoostPresent = tk.Radiobutton(self.Frame1, text='No',
                                                                variable=self.radioValueYesOrNo,
                                                                value=2)
            self.RadiobuttonNonBoostPresent.grid(row=self.i, column=2, padx=20)
            self.i = self.i+1

    def returnValueRadiobutton(self):
        return self.radioValueYesOrNo.get()

if __name__ == "__main__":
    app = Application()
    app.mainloop()

有人知道如何解决这个问题吗? 这是编写代码的最佳方式吗?

    def addWidgets(self):
        valueRadioButton = self.returnValueRadiobutton()
        if valueRadioButton == 1:
            label = tk.Label(self.Frame1, text='Select Yes or No')
            label.grid(row=self.i, column=0)
            print("creating")
            radioValueYesOrNo = tk.IntVar()
            RadiobuttonYesOrNo = tk.Radiobutton(self.Frame1, text='Yes',
                                                             variable=radioValueYesOrNo,
                                                             value=1,
                                                             command=lambda: self.addWidgets()
                                                             )
            RadiobuttonYesOrNo.grid(row=self.i, column=1)
            RadiobuttonNonBoostPresent = tk.Radiobutton(self.Frame1, text='No',
                                                             variable=radioValueYesOrNo,
                                                             value=2)
            RadiobuttonNonBoostPresent.grid(row=self.i, column=2, padx=20)
            self.i = self.i+1

我们想创建一个新的自己的按钮对象对吧?所以我们不能对不同的按钮使用相同的变量self.radioValueYesOrNoself.RadiobuttonYesOrNoself.RadiobuttonNonBoostPresent

是的,解决方案是在函数范围内创建它们的变量。希望对您有所帮助,玩得开心:)

你离得不远,但是你需要移动你对 self.i 的覆盖,这样你的 .grid 调用已经使用更新的值(编辑:我错了,您最初使用 0 并将 i 设置为 1,所以这应该没有问题)。一般来说,我建议您使用列表、字典或某种存储小部件的方式,以便您以后可以访问它们。我认为在您的示例中,您一直在覆盖相同的变量,这就是它被搞砸的原因。我修改了您的代码并添加了两个可选部分,您可以在其中删除已经生成新对的复选框的回调,或者甚至禁用旧的。但是,我希望我的解决方案清晰明了并能帮助您继续前进:

from tkinter import *
import tkinter as tk
from functools import partial


class Application(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)

        self.i = 1

        self.Frame1 = Frame(self)
        self.Frame1.grid(row=0, column=0)

        self.label = tk.Label(self.Frame1, text='Select Yes or No')
        self.label.grid(row=0, column=0)

        self.values = []
        self.radiobuttons_yes = []
        self.radiobuttons_no = []

        self.add_radiobuttons()  # adds the first pair

    def add_radiobuttons(self):
        """Adds a new set of radiobuttons and appends radiobutton_callback with the current index as command"""
        self.values.append(tk.IntVar())
        self.radiobuttons_yes.append(tk.Radiobutton(self.Frame1, text='Yes',
                                                    variable=self.values[-1],
                                                    value=1,
                                                    command=partial(self.radiobutton_callback, len(self.values) - 1)))
        self.radiobuttons_yes[-1].grid(row=self.i, column=0)
        self.radiobuttons_no.append(tk.Radiobutton(self.Frame1, text='No',
                                                   variable=self.values[-1],
                                                   value=2,
                                                   command=partial(self.radiobutton_callback, len(self.values) - 1)))
        self.radiobuttons_no[-1].grid(row=self.i, column=1)
        self.i += 1

    def radiobutton_callback(self, index):
        """Callback function for the radiobuttons"""
        if self.values[index].get() == 1:
            # optional: delete command of current radiobuttons to prevent multiple calls
            self.radiobuttons_yes[index].config(command='')
            self.radiobuttons_no[index].config(command='')

            # # optional2: set previous radiobuttons to inactivate
            # self.radiobuttons_yes[index].config(state=DISABLED)
            # self.radiobuttons_no[index].config(state=DISABLED)

            # add new radiobuttons
            self.add_radiobuttons()


if __name__ == "__main__":
    app = Application()
    app.mainloop()