[英]Python 2.7 GUI using Tkinter
I'm totally new to Python and GUI. 我对Python和GUI完全陌生。 I am trying to create a simple GUI using Tkinter which has two file browser options and a command button. 我正在尝试使用Tkinter创建一个简单的GUI,它具有两个文件浏览器选项和一个命令按钮。
When I try to click on the first browse button and select the file, the file name is displayed in both text boxes. 当我尝试单击第一个浏览按钮并选择文件时,文件名将显示在两个文本框中。 Similarly, when I click on the second button, the new file name is displayed in both text boxes. 同样,当我单击第二个按钮时,新的文件名将显示在两个文本框中。 Please correct me where I'm going wrong. 请纠正我要去哪里。 I tried much to fix but in vein. 我尽了很多努力来修复,但要顺其自然。 Also, Please pour in suggestions to improve my coding to reduce the LOC for such a simple 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()
Instead of creating object of StringVar class you have just assigned class to those variable 您没有为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()
Since you are using Entry widget, you don't need textvariable at all. 由于使用的是Entry小部件,因此根本不需要textvariable。 Just remove them and use widget's own methods. 只需删除它们,然后使用小部件自己的方法即可。
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()
But if you really insist on using it, you can change value of StringVar
by using file_path_1.set("your value")
. 但是,如果您真的坚持使用它,则可以使用file_path_1.set("your value")
来更改StringVar
的file_path_1.set("your value")
。
Your usage right now ( file_path_1 = "value"
) changes file_path_1
to a plain string. 现在的用法( file_path_1 = "value"
)将file_path_1
更改为纯字符串。
def open_rules_file():
var.set(askopenfilename())
var = StringVar()
entry1 = Entry(f1, width=70, textvariable = var)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.