如何使 result_lbl 的结果从 window 1 变为 window 2?

how to make the result from result_lbl from window 1 to window 2?

如何访问 result_lbl 的 window 1 到 window 2 的输出?我一直在阅读,似乎我需要在某处使用 "controller" 来 link 2 类 一起使用,但似乎 "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()

关于您的程序,我想评论的内容很少,但是,我只会提及与您的实际问题直接相关的内容。

重新设计您的程序并减少代码

更好的设计意味着更少的代码。关于这个发展的名字verse,你的第二个class:

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)

对于你想要达到的目标完全没用。您将在此 class 中实现的所有内容都可以在您使用 Toplevel().

创建的 self.second_window 框架中完成

所以只需将Window_2 class的配置注入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 .随着您的代码变得越来越长,这可能会导致混淆或最终出现错误,这不仅会影响将处理您的代码的同事,也会影响您自己。

程序重新设计的好处

您可能猜到我们上面所做的重新设计对解决您的问题有直接的积极影响。这是因为你的第二个框架(或你所说的 window)self.second_window() 现在看起来就像一个简单的方法,现在可以简单地访问 Application() class 的任何成员,即self.result_lbl。这样,你的问题就差不多解决了。

诀窍

完成您的问题解决方案的诀窍是在您所谓的 第二个 window 中创建另一个标签,我们称它为 self.result_lbl_copy 我们需要在 self.second_wind 中显示,那么我们必须使用 cget() 方法将其文本设置为与 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() class 保留以前的 不好的 设计,那么解决方案不会改变那么多。

就在这一行之前:

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 只是一个参数,您可以用任何名称调用它,例如 begueradj。如果你不明白为什么,请告诉我。但首先按 运行 Enter

执行你的程序