如何通过entry.get()从一个子类ttk.frame到另一个子类ttk.frame

问题描述 投票:0回答:1

我有两个子类框架,MainFrame 和 SubFrame。 SubFrame 有一个条目小部件 (entry1) 用于输入时间,默认条目是 00:00。我想从MainFrame 获取在SubFrame 的entry1 框中输入的时间。如何从 MainFrame 获取 SubFrame 中的条目小部件数据?

#!/usr/bin/python3.9

import tkinter as tk
from tkinter import ttk

class MainFrame(ttk.Frame):
    def __init__(self, container):
        style_name = f'{id(self)}.TFrame' # build the style name using object ID
        super().__init__(container, style=style_name) # apply the style

        s = ttk.Style()
        s.configure(style_name, background='red')

        self.labelA = ttk.Label(self, text = "This is on MainFrame")
        self.labelA.grid(column=1, row=2, padx = 30, pady = 30)

        self.buttonA = tk.Button(self, text="get from subframe", command=self.get_time)
        self.buttonA.grid(column=1, row=3)

    def get_time(self):
        print (sf.entry1())
        
class SubFrame(ttk.Frame):
    def __init__(self, container):
        style_name = f'{id(self)}.TFrame' # build the style name using object ID
        super().__init__(container, style=style_name) # apply the style

        s = ttk.Style()
        s.configure(style_name, background='blue')

        self.labelB = ttk.Label(self, text = "This is on SubFrame")
        self.labelB.grid(column=1, row=2, padx = 30, pady = 30)

        self.buttonB = tk.Button(self, text="get within class", command=self.get_time)
        self.buttonB.grid(column=1, row=3)

        ## ENTRY STYLE
        style = {'fg': 'black', 'bg': 'white', 'font': 'Helvetica 14 bold', 'width':'6', 'bd':'2', 
                        'highlightbackground':"black", 'justify':'center', 'relief':'sunken',
                        'insertontime': '0', 'takefocus': '0' } # highlightcolor':"red"

        self.entry1_var=tk.StringVar()
        self.entry1_var.set("00:00")
        self.entry1=tk.Entry(self, textvariable=self.entry1_var, width=5, justify="center",)
        self.entry1.grid(column=2, row=3, padx=(0, 0), pady=(0,10))
        self.entry1.configure(style)
 
    ## works if called from within subclass
    def get_time(self):
        print (self.entry1.get())

class App(tk.Tk):
    def __init__(self):
        super().__init__()

        s = ttk.Style()

        unfocus = "gray"
        focus = "#F2C84B"

        s.theme_create( "custom", parent="alt", settings={
            "TNotebook": {"configure": {"tabmargins": [2, 5, 2, 0] } },
            "TNotebook.Tab": {
                "configure": {"padding": [5, 1], "background": unfocus },
                "map":       {"background": [("selected", focus)],
                          "expand": [("selected", [1, 1, 1, 0])] } } } )

        s.theme_use("custom")

        self.notebook = ttk.Notebook(self)

        self.Frame1 = MainFrame(self.notebook)
        self.Frame2 = SubFrame(self.notebook)

        self.notebook.add(self.Frame1, text='MainFrame')
        self.notebook.add(self.Frame2, text='SubFrame')

        self.notebook.pack(expand = 1, fill ="both")

        ##--------------------------------##

        self.geometry('800x500')
        #self.resizable(False, False)
        self.title("TABS")
           

if __name__ == '__main__':
    
    app = App()

    SubFrame(app)
    MainFrame(app)
    
    app.mainloop()
python-3.x tkinter tkinter-entry
1个回答
0
投票

您在 Frame1 和 Frame2 的声明问题之前有一个参考。

Frame1 没有引用 Frame2,因为在创建 Frame1 时它不存在。

给 Frame1 一个对 Frame2 的引用,你的问题就解决了。

例如,您可以尝试以下代码:


self.Frame1 = MainFrame(self.notebook)
self.Frame2 = SubFrame(self.notebook)

# create a link from frame2 to frame1 as other
self.Frame1.other = self.Frame2

然后将

MainFrame.get_time
更改为:

    def get_time(self):
        print(self.other.entry1.get())
© www.soinside.com 2019 - 2024. All rights reserved.