【问题标题】:Python - GUI (TKinter), getting variables from other functions without running the whole function aganPython - GUI(TKinter),从其他函数获取变量而不运行整个函数 agan
【发布时间】:2016-10-03 12:15:31
【问题描述】:

我确实有一个无法真正解决的问题。问题是,我确实有两个单独的按钮。在一个按钮中,我想加载一个选定的文件。和另一个我想做一些搜索。 但是,如果不再次运行整个函数,我就无法将变量从一个函数传递到另一个函数。也就是说,查找按钮现在没用了。

from __future__ import print_function
from Tkinter import *
from Tkinter import Tk
from tkFileDialog import askopenfilename


def openFile():
    Tk().withdraw()
    txtFile = askopenfilename(defaultextension=".txt", filetypes=(("something", "*.txt"),("All Files", "*.*") ))
    print(txtFile)
    return txtFile


def Function():
    txtFile = openFile()
    with open(txtFile) as fp, open(('c:/map/test.txt'), 'w') as fo:
        for line in fp:

            if ('Hello') in line:
                content = line.strip() + " Hello detected "
            else:
                content = line.strip()
            fo.write(content + "\n")


def presentGUI():
    root = Tk()
    root.title("simulation")

    # Buttons
    button1 = Button(root, text="Select .txt file", command=openFile)
    button2 = Button(root, text="Run !", width=28, command=Function)

    # grid
    button1.grid(row=1, column=1)
    button2.grid(row=3, columnspan=3)

    root.mainloop()

presentGUI()

【问题讨论】:

    标签: python function variables tkinter


    【解决方案1】:

    两种方法

    1. 为 txtFile 使用全局变量
    2. 使用 OO 并将函数创建为类的一部分,以便它们可以共享变量。

    我已经为您提供了选项 2 的示例。因为这是我首选的工作方法。

    如果您单击“运行!”并且没有首先单击“选择 .txt 文件”,它会提示您选择一个文件。 您可以从“Application”类中的任何方法访问 self.txtFile 变量。

    from __future__ import print_function
    from Tkinter import *
    from Tkinter import Tk
    from tkFileDialog import askopenfilename
    
    class Application(Frame):
        def __init__(self,parent,**kw):
            Frame.__init__(self,parent,**kw)
            self.txtFile = None
            self.presentGUI()
    
        def openFile(self):
            ##Tk().withdraw()
            self.txtFile = askopenfilename(defaultextension=".txt", filetypes=(("something", "*.txt"),("All Files", "*.*") ))
            print(self.txtFile)
    
        def Function(self):
            if self.txtFile == None:
                self.openFile()
            with open(self.txtFile) as fp, open(('c:/map/test.txt'), 'w') as fo:
                for line in fp:
    
                    if ('Hello') in line:
                        content = line.strip() + " Hello detected "
                    else:
                        content = line.strip()
                    fo.write(content + "\n")
    
    
        def presentGUI(self):      
            # Buttons
            self.button1 = Button(self, text="Select .txt file", command=self.openFile)
            self.button2 = Button(self, text="Run !", width=28, command=self.Function)
    
            # grid
            self.button1.grid(row=1, column=1)
            self.button2.grid(row=3, columnspan=3)
    
    if __name__ == '__main__':
        root = Tk()
        root.title("simulation")
        app = Application(root)
        app.grid()
        root.mainloop()
    

    【讨论】:

    • 非常感谢!完全按照我想要的方式工作。永远不要真正进入课堂,继续努力!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-06
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多