I want to call GUI class
in B.py by button Measure
in A.py. It works,but I find something strange in class B
1.entry doesn't show text "test" that defined by self.var_PSA
.
2.Presses button PSA
, but the content of entry doesn't change too.
If I run B.py, all funcions work well. How could I solve the problem?
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()
You should change the self.var_PSA = tk.StringVar()
to self.var_PSA = tk.StringVar(master=self)
in B.py
file. You have a Tk()
instance in A.py
and another one in B.py
. You should tell to widgets that they are where connected in case of two Tk()
.
I recommend to use Toplevel()
instead of second Tk()
instance in case of multi window application. Read more: http://effbot.org/tkinterbook/toplevel.htm
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.