Python 为什么我不能使用Tkinter将Radiobutton变量从一个类传递到不同的类?

Python 为什么我不能使用Tkinter将Radiobutton变量从一个类传递到不同的类?,python,oop,tkinter,radio-button,Python,Oop,Tkinter,Radio Button,首先让我说,我已经搜索了堆栈溢出,但我无法找到问题的答案。我发现了一些非常接近的东西,但我仍然无法解决我的问题。如果你知道一个链接,这将是有益的,请包括在评论中 我对Python编程比较陌生,所以如果您看到任何不正确的编程实践,或者任何东西都可以用Python风格编程,请提供建设性的批评 我遇到了一个问题,从一个类中的单选按钮获取变量,然后在另一个类中的不同帧上的标签中打印出来。仅供参考:目前,所有课程都在同一个文件中,但我计划在完成课程后更改该文件 这是我的控制器类: import tkint

首先让我说,我已经搜索了堆栈溢出,但我无法找到问题的答案。我发现了一些非常接近的东西,但我仍然无法解决我的问题。如果你知道一个链接,这将是有益的,请包括在评论中

我对Python编程比较陌生,所以如果您看到任何不正确的编程实践,或者任何东西都可以用Python风格编程,请提供建设性的批评

我遇到了一个问题,从一个类中的单选按钮获取变量,然后在另一个类中的不同帧上的标签中打印出来。仅供参考:目前,所有课程都在同一个文件中,但我计划在完成课程后更改该文件

这是我的控制器类:

import tkinter as tk
from tkinter import ttk

class MainWindow(tk.Tk):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.emotion_list = ["angry", "sad", "happy", "fearful", "surprised", "curious",
                             "anxious", "chill"] 

        self.title("Application")

        self.frames = {}

        inner_container = ttk.Frame(self)
        inner_container.grid(sticky='ew')

        for FrameClass in (ChooseFeelings, ChooseState):
            frame = FrameClass(inner_container, self, self.pages)
            self.frames[FrameClass] = frame
            frame.grid(row=0, column=0, sticky='nsew')

        self.show_frame(ChooseFeelings)

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

    def get_frame(self, classname):
        """Returns an instance of a frame given its class name as a string"""
        for page in self.frames.values():
            if str(page.__class__.__name__) == classname:
                return page
        return None
如您所见,我在控制器类的底部包含了一个方法,可以在需要时调用该方法来创建任何类的实例

我还没有在self.frames字典中包含所有类,因为它们与当前问题无关

在这里,我有一个向用户显示单选按钮的类。我很清楚,就目前的情况来看,它们并没有组织在框架上。框架上还有一些我从类中忽略的小部件,因为它们与问题无关

class ChooseFeelings(ttk.Frame):
    def __init__(self, container, controller, **kwargs):
        super().__init__(container, **kwargs)

        emotion_container = ttk.Frame(self, padding='100 15 30 15')
        emotion_container.grid(sticky='ew')

        self.controller = controller
        self.selected_emotion = tk.StringVar()

        move_on_button = ttk.Button(emotion_container, text="Click me when you are ready to move on",
                                    command=lambda: [controller.show_frame(ChooseState), print(selected_emotion.get())])

        move_on_button.pack(side='top')

        for emotions in self.controller.emotion_list:
            ttk.Radiobutton(emotion_container, text=emotions, variable=self.selected_emotion,
                            value=emotions).pack(side='bottom')
单击move_on_按钮后,我将希望在下一帧标签中得到的变量打印到控制台

这是变量应该打印给用户的框架

class ChooseState(ttk.Frame):
    def __init__(self, container, controller, **kwargs):
        super().__init__(container, **kwargs)

        self.controller = controller

        state_container = ttk.Frame(self, padding='100 15 30 15')
        state_container.grid(sticky='ew')

        self.emotion_label = ttk.Label(state_container, text=f"You chose {self.get_feels()} as your emotion.")

        self.emotion_label.grid(row=0, column=0, sticky='w')

    def get_feels(self):
        feelings_frame = self.controller.get_frame("ChooseFeelings")
        user_emotion = feelings_frame.selected_emotion.get()
        return user_emotion


root = MainWindow()
root.mainloop()

如您所见,get\u feels方法用于创建ChooseFeelations类的实例,然后变量“user\u emotion”应该包含所选的单选按钮变量。一旦发生这种情况,将返回“user_emotion”变量,并将其打印到屏幕上。或者我是这么想的

在ChooseState类中,我应该在“self.emotion_label”中打印出上一个类中选择的单选按钮变量。相反,我得到的只是“你选择了你的情感”。我想做的就是将单选按钮变量从上一个类传递到这个类,而不出现任何错误

如果有人对如何解决这个问题有任何见解,我们将不胜感激。谢谢大家!