在函数中怎么访问entry中的内容
我想将以下代码mian函数中的那一坨代码放到win_tk()函数中去该怎么改啊,如果直接放进去的话run_com()函数中etyText.get()就没办法获取到entry中的内容
import tkinter as tk
from tkinter.filedialog import askopenfilename
from tkinter.messagebox import *
def win_tk():
pass
def select_com():
etyText.set(askopenfilename())
def run_com():
filePath = etyText.get()
showinfo('Tips', filePath)
if __name__ == '__main__':
root = tk.Tk()
root.attributes('-alpha', 0.9)
root.geometry('700x200+400+200')
etyText = tk.StringVar()
lb = tk.Label(root, font=('Arial', 10), text=' File Path :')
ety = tk.Entry(root, font=('Arial', 10), textvariable=etyText, width=80)
btnSelect = tk.Button(root, font=('Arial', 10), text='Select', command=select_com)
btnRun = tk.Button(root, font=('Arial', 10), fg='white', bg='gray', text=' Run ', command=run_com)
lb.grid(row=0, column=0)
ety.grid(row=0, column=1)
btnSelect.grid(row=0, column=2)
btnRun.grid(row=1, column=1)
tk.mainloop()
import tkinter as tk
from tkinter.filedialog import askopenfilename
from tkinter.messagebox import *
def win_tk():
root = tk.Tk()
root.attributes('-alpha', 0.9)
root.geometry('700x200+400+200')
etyText = tk.StringVar()
lb = tk.Label(root, font=('Arial', 10), text=' File Path :')
ety = tk.Entry(root, font=('Arial', 10), textvariable=etyText, width=80)
btnSelect = tk.Button(root, font=('Arial', 10), text='Select', command=select_com)
btnRun = tk.Button(root, font=('Arial', 10), fg='white', bg='gray', text=' Run ', command=run_com)
lb.grid(row=0, column=0)
ety.grid(row=0, column=1)
btnSelect.grid(row=0, column=2)
btnRun.grid(row=1, column=1)
tk.mainloop()
def select_com():
etyText.set(askopenfilename())
def run_com():
filePath = etyText.get()
showinfo('Tips', filePath)
if __name__ == '__main__':
win_tk()
本帖最后由 ZhKQYu 于 2022-11-23 12:53 编辑
import tkinter as tk
from tkinter.filedialog import askopenfilename
from tkinter.messagebox import *
from functools import partial
def win_tk():
root = tk.Tk()
root.attributes('-alpha', 0.9)
root.geometry('700x200+400+200')
etyText = tk.StringVar()
lb = tk.Label(root, font=('Arial', 10), text=' File Path :')
ety = tk.Entry(root, font=('Arial', 10), textvariable=etyText, width=80)
# 两种方法都行
# btnSelect = tk.Button(root, font=('Arial', 10), text='Select', command=partial(select_com, etyText))
btnSelect = tk.Button(root, font=('Arial', 10), text='Select', command=lambda: select_com(etyText))
btnRun = tk.Button(root, font=('Arial', 10), fg='white', bg='gray', text=' Run ', command=partial(run_com, etyText))
# btnRun = tk.Button(root, font=('Arial', 10), fg='white', bg='gray', text=' Run ', command=lambda: run_com(etyText))
lb.grid(row=0, column=0)
ety.grid(row=0, column=1)
btnSelect.grid(row=0, column=2)
btnRun.grid(row=1, column=1)
tk.mainloop()
def select_com(etyText):
etyText.set(askopenfilename())
def run_com(etyText):
filePath = etyText.get()
showinfo('Tips', filePath)
if __name__ == '__main__':
win_tk()
较小的改动:改造函数,用函数参数传递所需要处理的对象。若有必要使用全局变量也行。 根据需要返回适当的对象。
较大的改动:用OOP面向对象的方式,改写和实现。
import tkinter as tk
from tkinter.filedialog import askopenfilename
from tkinter.messagebox import *
def select_com():
etyText . set(askopenfilename())
def run_com():
filePath = etyText.get()
showinfo('Tips', filePath)
def win_tk():
global etyText # 添加此句,把 etyText 置为全局变量
root = tk.Tk()
root.attributes('-alpha', 0.9)
root.geometry('700x200+400+200')
etyText = tk.StringVar()
lb = tk.Label(root, font=('Arial', 10), text=' File Path :')
ety = tk.Entry(root, font=('Arial', 10), textvariable=etyText, width=80)
btnSelect = tk.Button(root, font=('Arial', 10), text='Select', command=select_com)
btnRun = tk.Button(root, font=('Arial', 10), fg='white', bg='gray', text=' Run ', command=run_com)
lb.grid(row=0, column=0)
ety.grid(row=0, column=1)
btnSelect.grid(row=0, column=2)
btnRun.grid(row=1, column=1)
tk.mainloop()
if __name__ == '__main__':
win_tk()
页:
[1]