python - Tkinter:调用另一个 GUI 窗口但小部件没有响应
问题描述
我想GUI class
通过 A.py 中的按钮调用Measure
B.py。它有效,但我发现一些奇怪的东西class B
1.entry 不显示由self.var_PSA
.
2.按下按钮PSA
,但输入的内容也没有改变。
如果我运行 B.py,所有功能都运行良好。我该如何解决这个问题?
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()
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()
解决方案
您应该将文件更改self.var_PSA = tk.StringVar()
为self.var_PSA = tk.StringVar(master=self)
。B.py
您有一个Tk()
实例A.py
,另一个实例位于B.py
. 您应该告诉小部件它们在两个Tk()
.
在多窗口应用程序的情况下,我建议使用Toplevel()
而不是第二个Tk()
实例。阅读更多:http ://effbot.org/tkinterbook/toplevel.htm
推荐阅读
- javascript - 在关闭页面之前,Beforeunload 需要与网页进行一些交互
- python - 显示方程式结果的Tkinter动态标签?
- android - 适用于 API < 23 的 Android 密钥生成器
- php - 仅在用户首次登录时显示弹出窗口(Laravel)
- javascript - 在不重新渲染代码的情况下更新 React-Native 模态的可见性
- flutter - 颤振中的错误:widget_test.dart 无法检测到 MyApp() 请帮助我
- identityserver4 - 身份服务器 4. 资源所有者密码流程中的身份资源声明
- azure - 在本地使用 SQL Server 运行 ACI
- jquery - 在没有预览的情况下打印 2 个 html 部分的副本
- python - 创建一组包含该组作为属性的类对象?