Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么不';t单选按钮在具有多帧的Tkinter窗口中工作?_Python_Tkinter_Radio Button_Frame - Fatal编程技术网

Python 为什么不';t单选按钮在具有多帧的Tkinter窗口中工作?

Python 为什么不';t单选按钮在具有多帧的Tkinter窗口中工作?,python,tkinter,radio-button,frame,Python,Tkinter,Radio Button,Frame,我复制了Python代码来创建一个包含多个帧的Tkinter窗口。我将许多种类的小部件放入其中,没有任何问题,但是当我添加单选按钮时,这些小部件的行为很有趣,尽管它们在常规窗口中工作良好(没有多个页面)。无论我是否设置该值,都不会选择任何单选按钮。更糟糕的是,如果我只是把鼠标指针放在一个单选按钮上,它看起来像是被选中了,尽管我没有点击它。如果我将鼠标指针移到两个单选按钮上,它们看起来都是选中的,这违反了单选按钮的选择规则 我应该补充一点,我用一个包管理器和一个网格管理器进行了尝试。结果是一样的

我复制了Python代码来创建一个包含多个帧的Tkinter窗口。我将许多种类的小部件放入其中,没有任何问题,但是当我添加单选按钮时,这些小部件的行为很有趣,尽管它们在常规窗口中工作良好(没有多个页面)。无论我是否设置该值,都不会选择任何单选按钮。更糟糕的是,如果我只是把鼠标指针放在一个单选按钮上,它看起来像是被选中了,尽管我没有点击它。如果我将鼠标指针移到两个单选按钮上,它们看起来都是选中的,这违反了单选按钮的选择规则

我应该补充一点,我用一个包管理器和一个网格管理器进行了尝试。结果是一样的

以下是我的代码的精简版本:

import tkinter as tk

class MainWindow(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        # Set the title of the main window.
        self.title('Multi-frame window')
        # Set the size of the main window to 300x300 pixels.
        self.geometry('300x100')

        # This container contains all the pages.
        container = tk.Frame(self)
        container.grid(row=1, column=1)
        self.frames = {} # These are pages to which we want to navigate.

        # For each page...
        for F in (StartPage, PageOne):
            # ...create the page...
            frame = F(container, self)
            # ...store it in a frame...
            self.frames[F] = frame
            # ..and position the page in the container.
            frame.grid(row=0, column=0, sticky='nsew')

        # The first page is StartPage.
        self.show_frame(StartPage)

    def show_frame(self, name):
        frame = self.frames[name]
        frame.tkraise()

class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text='Start Page')
        label.grid(row=1, column=1)

        # When the user clicks on this button, call the
        #   show_frame method to make PageOne appear.
        button1 = tk.Button(self, text='Visit Page 1',
            command=lambda : controller.show_frame(PageOne))
        button1.grid(row=2, column=1)

class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        # When the user clicks on this button, call the
        #   show_frame method to make StartPage appear.
        button1 = tk.Button(self, text='Back to Start',
            command=lambda : controller.show_frame(StartPage))
        button1.grid(row=1, column=1)
        options_label = tk.Label(self, text='Choose an option: ')
        options_label.grid(row=2, column=1)
        options_value = tk.IntVar()

        first_option = tk.Radiobutton( self , text = 'Option 1' ,
            variable = options_value , value = 1 )
        second_option = tk.Radiobutton( self , text = 'Option 2' ,
            variable = options_value , value = 2 )
        first_option.grid(row=2, column=2)
        second_option.grid(row=2, column=3)
        options_value.set(1)

if __name__ == '__main__':
    app = MainWindow()
    app.mainloop()

问题在于,
options\u value
是一个本地值,当
\uuuuu init\uuuu
完成时会被销毁


您需要保存对它的引用,例如
self.options\u value

问题在于,
选项\u值
是一个本地值,在
初始化\uuuuuu
完成时会被销毁


您需要保存对它的引用,例如
self.options\u value

问题是什么还不清楚。当我运行您的代码时,我可以选择一个单选按钮,当我返回开始,然后再次使用单选按钮返回页面时,我选择的值仍然处于选中状态。您确定此代码会产生您在问题中描述的行为吗?尽管我无法复制您的问题,但您的平台上可能存在问题,因为
options\u value
是一个局部变量,在
\u init\u
方法完成运行后会被销毁。是的!我用“self.options\u value”替换了“options\u value”,现在它可以工作了!非常感谢。(我会“接受”答案,但这是我第一次使用stackoverflow.com,我不知道如何“接受”。)不清楚问题出在哪里。当我运行您的代码时,我可以选择一个单选按钮,当我返回开始,然后再次使用单选按钮返回页面时,我选择的值仍然处于选中状态。您确定此代码会产生您在问题中描述的行为吗?尽管我无法复制您的问题,但您的平台上可能存在问题,因为
options\u value
是一个局部变量,在
\u init\u
方法完成运行后会被销毁。是的!我用“self.options\u value”替换了“options\u value”,现在它可以工作了!非常感谢。(我会“接受”答案,但这是我第一次使用stackoverflow.com,我不知道如何“接受”。)