我是GUI编程的新手。我做了一个小GUI,用户浏览文件,然后用文件执行他想要的功能。
现在我在GUI中有一个Entry Box,每当用户浏览所需文件时,在他完成浏览后,该文件的路径应出现在输入框中。
基本上我想要这样
浏览
浏览
我该怎么做?
我的GUI代码是:
#!/usr/bin/python
import Tkinter as tk
from tkFileDialog import *
import os
class StageGui:
prot=0
log=0
cwd=os.getcwd()
def __init__(self,parent,tex):
self.tex=tex
self.fm=tk.Frame(remodelmain)
self.l1=tk.Label(self.fm, text='Import .prot File').grid(row=0,column=0,sticky='w',padx=5,pady=10)
self.l2=tk.Label(self.fm, text='Import .log File').grid(row=1,column=0,sticky='w',padx=5,pady=10)
self.protentry = tk.Entry(self.fm, width = 50).grid(row=0,column=1,sticky='w',padx=5,pady=10)
self.logentry = tk.Entry(self.fm, width = 50).grid(row=1,column=1,sticky='w',padx=5,pady=10)
self.b1=tk.Button(self.fm, text='Browse',height=1,width=20,command=self.callback).grid(row=0,column=2,sticky='e',padx=5,pady=10)
self.b2=tk.Button(self.fm, text='Browse',height=1,width=20,command=self.callback1).grid(row=1,column=2,sticky='e',padx=5,pady=10)
self.fm.pack()
self.fm0=tk.Frame(remodelmain,width=500,height=500)
self.b3=tk.Button(self.fm0, text='Check',height=1,width=15,command=self.check).grid(row=4,column=2,sticky='e',padx=5,pady=10)
self.b4=tk.Button(self.fm0, text='Inertia',height=1,width=15).grid(row=5,column=2,sticky='e',padx=5,pady=10)
self.b5=tk.Button(self.fm0, text='Summary',height=1,width=15).grid(row=6,column=2,sticky='e',padx=5,pady=10)
self.b6=tk.Button(self.fm0, text='Report',height=1,width=15).grid(row=7,column=2,sticky='e',padx=5,pady=10)
self.fm0.pack(side='right')
self.fm1=tk.Frame(remodelmain,width=200,height=200)
self.tex1= tk.Text(self.fm1,width=130,height=10)
self.l3=tk.Label(self.fm1, text='Status Box:').grid(row=6,column=0,sticky='nw')
self.tex1.grid(row=6,column=1,sticky='s',padx=20,pady=10)
self.fm1.pack(side='left',anchor='w')
def callback(self):
name= askopenfilename()
StageGui.prot=name
self.printstatements(StageGui.prot)
def printstatements(self,name):
self.tex1.insert('end','\nthe file has been imported \n')
s='the path of the imported file is {}\n'.format(name)
self.tex1.insert('end',s)
self.tex1.see(tk.END)
return
def callback1(self):
name1= askopenfilename()
StageGui.log=name1
self.printstatements(StageGui.log)
def check(self):
file=open(StageGui.prot,'r')
a,b,c='|Checks|','|Status|','|Remarks|'
mess='\n{0:10s} \t {1:10s} \t {2:100s}\n'.format(a,b,c)
self.tex.insert('end',mess)
count_string_occurance(file)
remodelmain = tk.Tk()
fmn1=tk.Frame(remodelmain,width=300,height=300)
l3=tk.Label(fmn1, text='Message Box:').grid(row=6,column=0,sticky='nw')
tex= tk.Text(fmn1,width=130,height=60)
tex.grid(row=6,column=1,sticky='s',padx=20,pady=20)
fmn1.pack(side='bottom',anchor='w')
stagegui=StageGui(remodelmain,tex)
remodelmain.title('prototype_remodel')
remodelmain.geometry('1200x1200+300+300')
remodelmain.mainloop()
答案 0 :(得分:1)
创建与Entry小部件关联的字符串变量:
self.pathVar = tk.StringVar(self.fm)
self.protentry = tk.Entry(self.fm, width = 50, textvariable=self.pathVar).grid(row=0,column=1,sticky='w',padx=5,pady=10)
然后在获取路径后,将变量设置为该路径:
name= askopenfilename()
self.pathVar.set(name)