Python 通过使用tkinter按钮命令,如何将变量从函数传递到主函数中的本地变量
我是python和Tkinter的新手,请忍受混乱的代码 目前我有两个按钮(一个是打开文件,第二个是运行),和一个文本框(输入或滚动文本),我想做的是Python 通过使用tkinter按钮命令,如何将变量从函数传递到主函数中的本地变量,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我是python和Tkinter的新手,请忍受混乱的代码 目前我有两个按钮(一个是打开文件,第二个是运行),和一个文本框(输入或滚动文本),我想做的是 按“打开文件”按钮,文件路径将显示在文本框中,该路径也将保存到main()中的局部变量“a”中 当我按下“运行”按钮时,程序将获取变量“a”的最新值并执行其他操作 我面临的问题是,当我单击“打开文件”按钮时,无法更新此局部变量“a”。我搜索了这类问题,得到的答案是我应该使用“全局变量”或“生成一个类并将文件路径作为属性处理”。但不知怎的,我没能成
from functools import partial
from tkinter import scrolledtext,INSERT,END,Entry, messagebox, filedialog, ttk, Frame,Button
from os import path
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,NavigationToolbar2TkAgg
# Tkagg allows you to draw, navigation bar is to allow you save, zoomm ....the images
import numpy as np
from tkinter import *
def openfile(txt1):
global file1
file1= filedialog.askopenfilename(filetypes=(("all files","*.*"),("Text files","*.txt"),("csv file","*.csv")))
txt1.insert(0,file1)
# print (file1)
def print_f1():
print(file1)
def main ():
# Create Main window
window = Tk()
file1=''
window.title("WADC FFT trial")
window.minsize(1000,600)
input_str=StringVar()
input_str.set('')
# Create Frame for buttons, bars and picts
mis_frame = Frame(window)
mis_frame.grid(column=0, row=0)
#Create a scrolled text box on mis_frame
txt=Entry(mis_frame,textvariable=input_str,width=40)
txt.grid(column=0,row=0)
#define openfile
#Create a button on mis_frame, use lambda to avoid direct invoke the function called
# btn_path= Button(mis_frame,text="Open File",command=lambda:openfile(txt1))
#another workaround is to use partial funciton in python, assign a new func with lesser variables.
op_func=partial(openfile,txt)
btn_path=Button(mis_frame,text='Open File',command=op_func)
btn_path.grid(column =1,row =0)
script_path=input_str
print('script path is '+str(script_path))
btn_run = Button(mis_frame, text='Run',command=print_f1)
btn_run.grid(column=3,row=0,padx=100)
window.mainloop()
if __name__=='__main__':
main()
其次是class属性。这一个我只做了一个按钮,可以打开文件和获取文件路径。
关键是我想把这个“文件名”传递给局部变量“a”,就像我运行代码一样,它会传递默认值“文件名”,在我点击“浏览”按钮后不会更新
感谢您提供的建议,可能会有所帮助
from tkinter import *
import tkinter as tk
class Canvas(tk.Tk): #inherient from Tkinter.tk
def __init__(self):
tk.Tk.__init__(self) #inheritate all attributes that tk has
self.filename ='' #declare filepath
tk.Button(self, text='Browse', command=self.openfile).grid(column=0,row=0)
def openfile(self):
self.filename = filedialog.askopenfilename(title="Open file")
def main ():
b=Canvas()
b.title("Test")
b.minsize(600,400)
a=b.filename
print(a)
b.mainloop()
if __name__=='__main__':
main()
问题
script_path=input_str
将StringVar
指定给脚本路径,而不是它的值。
要获取值,请使用script\u path=input\u str.get()
当这条线
a = b.filename
执行时,它将文件名的默认值分配给,因为您没有调用openfile
解决方案 要将值传递到全局范围,既不需要类也不需要全局关键字。 只需使用tkinter变量
file1 = StringVar()
def openfile(txt1):
file1.set(filedialog.askopenfilename(filetypes=( ("all files","*.*"),("Text files","*.txt"),("csv file","*.csv") ) ) )
txt1.insert(0,file1)
或者让你的函数返回一些东西
def openfile(txt1):
file1 = filedialog.askopenfilename(filetypes=( ("all files","*.*"),("Text files","*.txt"),("csv file","*.csv") ))
txt1.insert(0,file1)
return file1
在您的示例代码中,他们没有导入另一个模块。对不起,我不太清楚您在说什么。我需要导入哪个模块?你能详细说明一下吗?谢谢你贴了两个例子。它们在同一个
.py
文件中吗?首先你必须理解。通读