【发布时间】: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