以下是代码:
from tkinter import *
from tkinter.filedialog import askopenfilename
root = Tk()
p = None
def set_text(text):
global p
filePath.delete(0,END)
filePath.insert(0,text)
p = filePath.get()
return
def get_path():
filename = askopenfilename()
json1_file = open(filename)
return filename
root['bg'] = '#fafafa'
root.title('json parser')
root.geometry('500x300')
root.resizable(width=False, height=False)
frame_top = Frame(root, bg='#ffb700', bd=5)
frame_top.place(relx=0.15, rely=0.15, relwidth=0.7, relheight=0.25)
frame_bottom = Frame(root, bg='#ffb700', bd=5)
frame_bottom.place(relx=0.15, rely=0.55, relwidth=0.7, relheight=0.1)
filePath= Entry(frame_top, bg='white', font=30)
filePath.pack(side=LEFT)
b1 = Button(frame_top,text="choose file",command=lambda:set_text(get_path()))
b1.pack(side=RIGHT)
print(p)
info = Label(frame_bottom, text='programm', bg='#ffb700', font=40)
info.pack()
root.mainloop()
我是编程新手,这是我第一次接触tkinter。
我需要filePath.get()
函数的结果来在函数外部使用它。我尝试将其作为全局变量分配给p
变量,但没有成功。我做错了什么
p变量赋值没有问题。p已经有值了。但必须注意mainloop()机制。在程序开始运行时,p没有值。按b1按钮后,p获取值但打印(p),不要再次调用,因为mainloop()只是刷新小部件,没有完整的代码
相关问题 更多 >
编程相关推荐