如何访问窗口1的result_lbl到窗口2的输出?我一直在阅读,似乎我需要使用" controller"某处将两个类连接在一起,但似乎与" controller"在某个地方需要有一个lambda函数,我认为这是不可能的。有人请帮助我。感谢。
import tkinter as t
def main():
root = t.Tk()
root.geometry("250x370")
root.config(background="LightBlue4")
root.title("print to screen")
app = Application(root)
root.mainloop()
class Application:
def __init__(self,parent):
self.parent=parent
self.frame_top = t.Frame(self.parent, bg="tan", bd=2, relief=t.RIDGE)
self.frame_top.pack(anchor=t.N, fill=t.BOTH, expand=1)
self.frame_btm = t.Frame(self.parent, bg="plum1", bd=2, relief=t.RIDGE)
self.frame_btm.pack(anchor=t.S, fill=t.X, expand=0)
self.labelVariable=t.StringVar()
self.result_lbl=t.Label(self.frame_top, font="Helvetica 50 bold italic", textvariable=self.labelVariable)
self.result_lbl.pack()
self.entryVariable = t.StringVar()
self.entry=t.Entry(self.frame_btm, textvariable=self.entryVariable)
self.entry.bind("<Return>", self.on_button)
self.entry.pack()
self.button = t.Button(self.frame_btm, text="print to screen", command=self.on_button)
self.button.pack()
self.button_window=t.Button(self.frame_btm, text="2nd", fg="brown",command=self.second_window)
self.button_window.pack()
def on_button(self):
self.labelVariable.set(self.entryVariable.get())
self.entry.delete(0,"end")
def second_window(self):
self.second_window = t.Toplevel(self.parent)
self.second_window.geometry("250x370")
self.second_window.title("2nd window")
self.app = Window_2(self.second_window)
class Window_2:
def __init__(self, parent):
self.parent = parent
self.frame= t.Frame(self.parent, bg="tan", bd=2, relief=t.RIDGE)
self.frame.pack(anchor=t.N, fill=t.BOTH, expand=1)
if __name__ == '__main__':
main()
答案 0 :(得分:-1)
我想对您的计划发表评论,但是,我只会提及与您的实际问题直接相关的内容。
更好的设计意味着更少的代码。关于这个开发的名称 verse ,你的第二课:
class Window_2:
def __init__(self, parent):
self.parent = parent
self.frame= t.Frame(self.parent, bg="tan", bd=2, relief=t.RIDGE)
self.frame.pack(anchor=t.N, fill=t.BOTH, expand=1)
对于您想要实现的目标完全没用。您将在此课程中实现的所有内容,您可以使用Toplevel()
在self.second_window
框架中执行此操作。
所以只需采用Window_2
类的配置并将其注入self.second_window
顶层框架。这样你就可以将第一个设计移动到后面。
self.second_window = t.Toplevel(self.parent, bg = 'tan', bd=2, relief=t.RIDGE)
另外,为了代码维护和可伸缩性,我建议您将self.second_window
方法中的变量self.second_window()
重命名为其他内容,让我们说self.second_win
。随着您的代码变得越来越长,这可能会引起混淆,或者最终的错误不仅会出现在您的代码上,也会影响到您自己的代码。
你可能会猜到我们上面做的重新设计对解决你的问题有直接的积极影响。这是因为你的第二个框架(或你所称的窗口)self.second_window()
看起来就像一个简单的方法,现在只能访问Application()
类的任何成员,即self.result_lbl
。这样,您的问题几乎已经解决了。
完成问题解决方案的技巧现在是在你调用第二个窗口的内容中创建另一个标签,让我们称之为self.result_lbl_copy
我们需要在{{ 1}}然后我们必须使用cget()
方法将其文本设置为与self.second_wind
中相同的文本。
我们的代码中反映了哪些内容:
self.result_lbl
以下是您可以测试的程序:
self.result_lbl_copy=t.Label(self.second_win, font="Helvetica 50 bold italic", textvariable=self.labelVariable, text = self.result_lbl.cget('text'))
self.result_lbl_copy.pack()
以下是正在运行的程序的屏幕截图:
如果你想使用'''
Created on Apr 21, 2016
@author: billal begueradj
'''
import tkinter as t
def main():
root = t.Tk()
root.geometry("250x370")
root.config(background="LightBlue4")
root.title("print to screen")
app = Application(root)
root.mainloop()
class Application:
def __init__(self,parent):
self.parent=parent
self.frame_top = t.Frame(self.parent, bg="tan", bd=2, relief=t.RIDGE)
self.frame_top.pack(anchor=t.N, fill=t.BOTH, expand=1)
self.frame_btm = t.Frame(self.parent, bg="plum1", bd=2, relief=t.RIDGE)
self.frame_btm.pack(anchor=t.S, fill=t.X, expand=0)
self.labelVariable=t.StringVar()
self.result_lbl=t.Label(self.frame_top, font="Helvetica 50 bold italic", textvariable=self.labelVariable)
self.result_lbl.pack()
self.entryVariable = t.StringVar()
self.entry=t.Entry(self.frame_btm, textvariable=self.entryVariable)
self.entry.bind("<Return>", self.on_button)
self.entry.pack()
self.button = t.Button(self.frame_btm, text="print to screen", command=self.on_button)
self.button.pack()
self.button_window=t.Button(self.frame_btm, text="2nd", fg="brown",command=self.second_window)
self.button_window.pack()
def on_button(self):
self.labelVariable.set(self.entryVariable.get())
self.entry.delete(0,"end")
def second_window(self):
self.second_win = t.Toplevel(self.parent, bg = 'tan', bd=2, relief=t.RIDGE)
self.second_win.geometry("250x370")
self.second_win.title("2nd window")
self.result_lbl_copy=t.Label(self.second_win, font="Helvetica 50 bold italic", textvariable=self.labelVariable, text = self.result_lbl.cget('text'))
self.result_lbl_copy.pack()
if __name__ == '__main__':
main()
类来保留以前的错误的设计,那么解决方案不会改变那么多。
就在这一行之前:
Window_2()
插入我上面提到的相同的两行来获取技巧:
self.app = Window_2(self.second_window)
以下是具有您自己设计的完整程序:
self.result_lbl_copy=t.Label(self.second_window, font="Helvetica 50 bold italic", textvariable=self.labelVariable, text = self.result_lbl.cget('text'))
self.result_lbl_copy.pack()
我测试了这个程序,它导致了相同的界面。您可以在两种解决方案中进行选择,但出于我提到的原因,我建议您使用第一种解决方案。
'''
Created on Apr 22, 2016
@author: billal begueradj
'''
import tkinter as t
def main():
root = t.Tk()
root.geometry("250x370")
root.config(background="LightBlue4")
root.title("print to screen")
app = Application(root)
root.mainloop()
class Application:
def __init__(self,parent):
self.parent=parent
self.frame_top = t.Frame(self.parent, bg="tan", bd=2, relief=t.RIDGE)
self.frame_top.pack(anchor=t.N, fill=t.BOTH, expand=1)
self.frame_btm = t.Frame(self.parent, bg="plum1", bd=2, relief=t.RIDGE)
self.frame_btm.pack(anchor=t.S, fill=t.X, expand=0)
self.labelVariable=t.StringVar()
self.result_lbl=t.Label(self.frame_top, font="Helvetica 50 bold italic", textvariable=self.labelVariable)
self.result_lbl.pack()
self.entryVariable = t.StringVar()
self.entry=t.Entry(self.frame_btm, textvariable=self.entryVariable)
self.entry.bind("<Return>", self.on_button)
self.entry.pack()
self.button = t.Button(self.frame_btm, text="print to screen", command=self.on_button)
self.button.pack()
self.button_window=t.Button(self.frame_btm, text="2nd", fg="brown",command=self.second_window)
self.button_window.pack()
def on_button(self):
self.labelVariable.set(self.entryVariable.get())
self.entry.delete(0,"end")
def second_window(self):
self.second_window = t.Toplevel(self.parent)
self.second_window.geometry("250x370")
self.second_window.title("2nd window")
self.result_lbl_copy=t.Label(self.second_window, font="Helvetica 50 bold italic", textvariable=self.labelVariable, text = self.result_lbl.cget('text'))
self.result_lbl_copy.pack()
self.app = Window_2(self.second_window)
class Window_2:
def __init__(self, parent):
self.parent = parent
self.frame= t.Frame(self.parent, bg="tan", bd=2, relief=t.RIDGE)
self.frame.pack(anchor=t.N, fill=t.BOTH, expand=1)
if __name__ == '__main__':
main()
方法我想阻止你发现你没有问过但我在你的程序中发现的错误:这个功能有问题:
on_button()
将其定义更改为:
def on_button(self):
self.labelVariable.set(self.entryVariable.get())
self.entry.delete(0,"end")
def on_button(self,something):
只是一个参数,您可以使用任何名称调用something
。如果你不明白为什么,请告诉我。但首先运行 Enter