【发布时间】:2017-12-08 16:21:24
【问题描述】:
我对 Python 和 GUI 完全陌生。我正在尝试使用 Tkinter 创建一个简单的 GUI,它有两个文件浏览器选项和一个命令按钮。
当我尝试单击第一个浏览按钮并选择文件时,文件名显示在两个文本框中。同样,当我单击第二个按钮时,新文件名会显示在两个文本框中。请纠正我哪里出错了。我尝试了很多修复,但在静脉中。另外,请提出建议以改进我的编码,以减少这种简单 GUI 的 LOC。
import os
import Tkinter
import tkFileDialog
from tkFileDialog import askopenfilename
from Tkinter import *
file_path_1 = ''
file_path_2 = ''
#~~~~ FUNCTIONS~~~~
def open_rules_file():
global file_path_1
filename1 = askopenfilename()
file_path_1 = os.path.abspath(filename1)
print file_path_1
entry1.delete(0, END)
entry1.insert(0, file_path_1)
def open_src_file():
global file_path_2
filename2 = askopenfilename()
file_path_2 = os.path.abspath(filename2)
print file_path_2
entry2.delete(0, END)
entry2.insert(0, file_path_2)
#~~~~~~ GUI ~~~~~~~~
root = Tk()
root.title('MY GUI')
root.geometry("1000x300+250+100")
mf = Frame(root)
mf.pack()
f1 = Frame(mf, width=600, height=250)
f1.pack(fill=X)
f2 = Frame(mf, width=600, height=250)
f2.pack()
f3 = Frame(mf, width=600, height=250)
f3.pack()
file_path_1 = StringVar
file_path_2 = StringVar
Label(f1,text="Select Rules Sheet (xls)").grid(row=1, column=0, sticky='e')
entry1 = Entry(f1, width=70, textvariable=file_path_1)
entry1.grid(row=1,column=1,padx=2,pady=2,sticky='we',columnspan=25)
Button(f1, text="Browse1", command=open_rules_file).grid(row=1, column=27, sticky='ew', padx=8, pady=4)
Label(f2,text="Select COBOL Source ").grid(row=2, column=0, sticky='e')
entry2 = Entry(f2, width=70, textvariable=file_path_2)
entry2.grid(row=2,column=1,padx=2,pady=2,sticky='we',columnspan=25)
Button(f2, text="Browse2", command=open_src_file).grid(row=2, column=27, sticky='ew', padx=8, pady=4)
Button(f3, text='Quit', command=f3.quit).grid(row=3, column=0, sticky=W, pady=4)
root.mainloop()
【问题讨论】:
-
无法在我的机器上重现该问题。
-
我已经更新了我的帖子以包含屏幕截图。请检查相同。
-
我也无法重现该问题,对我来说很好。
-
你为什么在你的代码中使用 StringVar ?
标签: python python-2.7 tkinter