繁体   English   中英

将 label 通过 tkinter 0

[英]Adding label to frame class via method from another class in tkinter 0

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

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

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()

现在您创建了 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM