在Python Gui的输入框中插入路径

2024-10-01 17:33:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我不熟悉GUI编程。我做了一个小的GUI,用户可以浏览文件,然后对文件执行他想要的功能。在

现在我在GUI中有一个输入框,每当用户浏览所需的文件,并且在他完成浏览之后,该文件的路径应该出现在输入框中。在

基本上我想要这样 浏览前before browsing {$img2}后浏览

我该怎么做?在

我对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()

Tags: textselfcolumnwidthtkgridrowtex
1条回答
网友
1楼 · 发布于 2024-10-01 17:33:57

创建与条目小部件关联的字符串变量:

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)

然后在获取路径后,将变量设置为该路径:

^{pr2}$

相关问题 更多 >

    热门问题