Tkinter:调用另一个GUI窗口,但小部件没有响应

2024-06-15 09:25:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我想通过A.py中的按钮Measure调用B.py中的GUI class。它是有效的,但我在class B中发现了一些奇怪的东西

1.条目不显示由self.var_PSA定义的文本“test”

2.按下按钮PSA,但条目的内容也不会改变

如果我运行B.py,所有函数都可以正常工作。我怎样才能解决这个问题

A.py

import tkinter as tk 
import B

class main_win(tk.Tk):

    def __init__(self):
        tk.Tk.__init__(self)
        self._frame = None
        self.show_frame(Page3)

    def show_frame(self, frame_class):
        new_frame = frame_class(self)
        if self._frame is not None:
            self._frame.destroy()
        self._frame = new_frame
        self._frame.pack()


class Page3(tk.Frame):
 
    def __init__(self, master):

        tk.Frame.__init__(self,master, width=900, height=620, bg = "#d8d8d8")
        self.master = master

        self.button_meas = tk.Button(self, command = lambda:self.CallB())
        self.button_meas.place(relx=0.278, rely=0.5, height=31, width=94)
        self.button_meas.configure(activebackground="#ececec")
        self.button_meas.configure(activeforeground="#000000")
        self.button_meas.configure(background="#d9d9d9")
        self.button_meas.configure(disabledforeground="#a3a3a3")
        self.button_meas.configure(foreground="#000000")
        self.button_meas.configure(highlightbackground="#d9d9d9")
        self.button_meas.configure(highlightcolor="black")
        self.button_meas.configure(pady="0")
        self.button_meas.configure(text='''Measure''')

    def CallB(self):

        B.GUI()

app = main_win()

app.mainloop()

B.py

import tkinter as tk

class GUI(tk.Tk):


    def __init__(self):

        tk.Tk.__init__(self)

        tk.Tk.geometry(self, "1200x820")

        self.Frame1 = tk.Frame(self)
        self.Frame1.place(relx=0.057, rely=0.147, relheight=0.263
                , relwidth=0.895)
        self.Frame1.configure(relief='groove')
        self.Frame1.configure(borderwidth="2")
        self.Frame1.configure(relief="groove")
        self.Frame1.configure(background="#d9d9d9")
        self.Frame1.configure(highlightbackground="#d9d9d9")
        self.Frame1.configure(highlightcolor="black")

        self.var_PSA = tk.StringVar()
        self.var_PSA.set("test")
        self.PSA_Entry = tk.Entry(self.Frame1,textvariable = self.var_PSA)
        self.PSA_Entry.place(relx=0.676, rely=0.465, height=31
                , relwidth=0.296)
        self.PSA_Entry.configure(background="white")
        self.PSA_Entry.configure(disabledforeground="#a3a3a3")
        self.PSA_Entry.configure(font="TkFixedFont")
        self.PSA_Entry.configure(foreground="#000000")
        self.PSA_Entry.configure(highlightbackground="#d9d9d9")
        self.PSA_Entry.configure(highlightcolor="black")
        self.PSA_Entry.configure(insertbackground="black")
        self.PSA_Entry.configure(selectbackground="#c4c4c4")
        self.PSA_Entry.configure(selectforeground="black")

        self.button_PSA = tk.Button(self.Frame1, command = lambda: self.PSA_event())
        self.button_PSA.place(relx=0.594, rely=0.465, height=31, width=70)
        self.button_PSA.configure(activebackground="#ececec")
        self.button_PSA.configure(activeforeground="#000000")
        self.button_PSA.configure(background="#d9d9d9")
        self.button_PSA.configure(disabledforeground="#a3a3a3")
        self.button_PSA.configure(foreground="#000000")
        self.button_PSA.configure(highlightbackground="#d9d9d9")
        self.button_PSA.configure(highlightcolor="black")
        self.button_PSA.configure(pady="0")
        self.button_PSA.configure(text='''PSA''')
    
    def PSA_event(self):
        self.var_PSA.set("hello")


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

Tags: pyselfinitconfiguredefbuttonframetk
1条回答
网友
1楼 · 发布于 2024-06-15 09:25:14

您应该在B.py文件中将self.var_PSA = tk.StringVar()更改为self.var_PSA = tk.StringVar(master=self)。在A.py中有一个Tk()实例,在B.py中有另一个实例。在两个Tk()的情况下,您应该告诉小部件它们在连接的地方

对于多窗口应用程序,我建议使用Toplevel()而不是第二个Tk()实例。阅读更多:http://effbot.org/tkinterbook/toplevel.htm

相关问题 更多 >