我是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()
答案 0 :(得分:0)
您没有创建StringVar类的对象,而是将类分配给那些变量
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()
##Instead of creating object of that class you have just assigned class to those variable
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()
答案 1 :(得分:0)
由于您正在使用Entry小部件,因此根本不需要textvariable。只需删除它们并使用widget自己的方法。
def open_rules_file():
filename1 = askopenfilename()
print(filename1)
entry1.delete(0, END)
entry1.insert(0, filename1)
def open_src_file():
filename2 = askopenfilename()
print(filename2)
entry2.delete(0, END)
entry2.insert(0, filename2)
#~~~~~~ GUI ~~~~~~~~
root = Tk()
root.title('MY GUI')
root.geometry("1000x300+250+100")
mf = Frame(root)
mf.pack()
f1 = Frame(mf, width=600, height=250)
f2 = Frame(mf, width=600, height=250)
f3 = Frame(mf, width=600, height=250)
f1.pack(fill=X)
f2.pack()
f3.pack()
Label(f1,text="Select Rules Sheet (xls)").grid(row=1, column=0, sticky='e')
entry1 = Entry(f1, width=70)
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)
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
更改file_path_1.set("your value")
的值。
您现在的使用情况(file_path_1 = "value"
)将file_path_1
更改为普通字符串。
def open_rules_file():
var.set(askopenfilename())
var = StringVar()
entry1 = Entry(f1, width=70, textvariable = var)