【问题标题】:Adding label to frame class via method from another class in tkinter 0通过tkinter 0中另一个类的方法将标签添加到框架类
【发布时间】:2020-12-31 20:57:00
【问题描述】:

我正在尝试将我的 tkinter 代码调整为 OOP 方法。到目前为止,框架已经创建,但现在被类之间的交互所困扰。 HelloWorld 类的 submit_name 函数由 InputFrame 中的提交按钮命令访问,但我不确定如何获取 InputFrame 条目和单选按钮选择中的内容,然后将该文本放入 OutputFrame 的新标签中。非常感谢任何建议或指导!

这是我的非 OOP 代码中的工作函数:

def submit_name():
    '''prints out message based on text and radio button selection'''
    if case_style.get() == 'normal':
        name_label = tk.Label(output_frame, text='Hello ' + name.get() + '! Keep on learning Tkinter!', bg=output_color)
    elif case_style.get() == 'upper':
        name_label = tk.Label(output_frame, text=('Hello ' + name.get() + '! Keep on learning Tkinter!').upper(), bg=output_color)
    name_label.pack()
    name.delete(0, END)

OOP 代码如下:

import tkinter as tk
from tkinter import ttk,StringVar, END

class HelloWorld(tk.Tk):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.title("Hello, World!")
        self.geometry('400x400')
        self.resizable(0,0)
        self_color = '#224870'
        self.config(bg=self_color)
        
        InputFrame(self).pack(pady=10)
        OutputFrame(self).pack(padx=10, pady=(0,10),fill='both',expand=True)        
    def submit_name(self):
        '''adjusts text case in entry widget based on radio button selection and prints to OutputFrame'''
        pass

class InputFrame(tk.Frame):
    def __init__(self, container,*args, **kwargs):
        super().__init__(container,*args, **kwargs)
        self.case_style=StringVar()
        self.case_style.set('normal')
        self.input_color = '#2a4494'
        self.config(bg=self.input_color)
        self.name=tk.Entry(self, width=20)
        self.name.grid(row=0,column=0, padx=10, pady=10)
        submit_button = tk.Button(self, text='Submit', command=container.submit_name)
        submit_button.grid(row=0,column=1,padx=10, pady=10, ipadx=20)
        normal_button = tk.Radiobutton(self, text='Normal Case',variable=self.case_style,value='normal',bg=self.input_color)
        normal_button.grid(row=1,column=0, padx=2, pady=2)
        upper_button = tk.Radiobutton(self, text='Upper Case',variable=self.case_style,value='upper',bg=self.input_color)
        upper_button.grid(row=1,column=1, padx=2, pady=2)
        

        
class OutputFrame(tk.Frame):
    def __init__(self,container,*args,**kwargs):
        super().__init__(container,*args,**kwargs)
        self.output_color = '#4ea5da'
        self.config(bg=self.output_color)

root = HelloWorld()
root.mainloop()

【问题讨论】:

    标签: python oop tkinter


    【解决方案1】:

    现在您创建了 Frame 对象,但随后您丢弃了对它们的引用,因此您无法再次使用它们。您需要更改代码以保留该引用,然后您可以从任何地方访问它们。

    class HelloWorld(tk.Tk):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.title("Hello, World!")
            self.geometry('400x400')
            self.resizable(0,0)
            self_color = '#224870'
            self.config(bg=self_color)
            
            self.input_frame = InputFrame(self)
            self.input_frame.pack(pady=10)
            self.output_frame = OutputFrame(self)
            self.output_frame.pack(padx=10, pady=(0,10),fill='both',expand=True)  
          
        def submit_name(self):
            '''adjusts text case in entry widget based on radio button selection and prints to OutputFrame'''
            data = self.input_frame.name.get()
            self.output_frame.output_label.config(text=data)
    

    【讨论】:

    • 你是救生员!!这现在完全有意义,并且能够无缝地传输功能代码。再次感谢您。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-31
    • 2015-01-31
    • 2016-01-16
    • 1970-01-01
    • 2013-02-10
    • 1970-01-01
    • 2023-03-03
    相关资源
    最近更新 更多