如何互动呼叫

时间:2017-04-23 22:13:06

标签: python function tkinter reference

我是Python的新手,当我尝试中断“mainloop”以暂停程序时,我正在尝试更新我的Tkinter框架“main”。我正在努力进行功能间引用,并想知道如何更新/或如何引用我的Tkinter框架的更新。

有些代码可能很笨拙,我可能在这里和那里都有逻辑错误,但主要的是功能间引用

#IMPORTS
import time
import tkinter as Tk

class AdventuresofTOT(Tk.Tk):

    def setup(self,Tk):
        main = Tk.PanedWindow(root)
        main.config(orient=Tk.VERTICAL)

        textwindow = Tk.Text(main)
        textwindow.config(relief=Tk.SUNKEN, font=("Consolas",10), height=57, width=65)
        main.add(textwindow)
        textwindow.pack()

        bottom = Tk.PanedWindow(main)
        bottom.config(orient=Tk.HORIZONTAL)
        main.add(bottom)
        bottom.pack(fill=Tk.X,side=Tk.BOTTOM)

        IWlabel = Tk.Label(bottom)
        IWlabel.config(text = "User Input")
        bottom.add(IWlabel)
        IWlabel.pack(fill=Tk.X, expand=1, side=Tk.LEFT)

        inputwindow = Tk.Entry(bottom)
        bottom.add(inputwindow)
        inputwindow.pack(fill=Tk.X, expand=1, side=Tk.RIGHT)

        main.pack()

    def write(self, Tk, textframe, textobject, n):
        for line in textobject:
            textframe.insert(Tk.INSERT, line + "\n")
            main.update()
            time.sleep(n)

    def sleepr(n):
        main.update()
        time.sleep(n)

    def init(self, Tk):
        Tk.Tk.__init__(self)
        AdventuresofTOT().setup(Tk)
        f = open('PhaseOne.txt', 'r')
        self.write(Tk, AdventuresofTOT().setup(Tk), f, 0.1)
        self.sleeper(3) 

root = AdventuresofTOT()
root.init(Tk)
root.mainloop()

编辑:这样我可以在Write函数中引用更新

def write(self, Tk, textframe, textobject, n):
    for line in textobject:
        textframe.insert(Tk.INSERT, line + "\n")
        main.update()
        time.sleep(n)

EDIT2.0:即参考以下代码

main.update()
#Examples below of referencing V obviously incorrect, hence question.
#setup.main.update()
#AdventuresofTOT.setup.main.update()

1 个答案:

答案 0 :(得分:0)

发现我需要使用全局变量才能跨函数使用变量!