简体   繁体   English

更改 Tkinter 上的条目变量

[英]Changing Entry variable on Tkinter

so I am trying to get the file path from the file directory using tkinter.所以我正在尝试使用 tkinter 从文件目录中获取文件路径。 My current script doesn't raise any errors but it's not doing what I want it to do.我当前的脚本没有引发任何错误,但它没有做我想要做的事情。 So I created an input with text inside and I want to swap that text out for the file path, but also give the option of typing/editing it out before and after the text is changed.因此,我创建了一个内部带有文本的输入,并且我想将该文本换成文件路径,但还提供在文本更改之前和之后键入/编辑它的选项。 This is what I have right now这就是我现在所拥有的

from tkinter import *
from tkinter import simpledialog
from tkinter import filedialog

class Initial(simpledialog.Dialog):
    def browseFiles(self):
        filename1 = filedialog.askopenfilename(initialdir = "/",
                                               title = "Select a File",
                                               filetypes = (("Pictures",
                                                             "*.jpg*"),
                                                            ("All files",
                                                             "*.*")))
        self.e3.configure(textvariable=filename1)

    def body(self, master):
        Label(master, text="Picture path:").grid(row=4)
        Label(master, text="...").grid(row=3, column=2)
        Label(master, text="Picture 2 path:").grid(row=5)

        text = tk.StringVar()
        text.set('Enter or browse picture/path')
        #input fields for tags
        #Entry fields
        self.e1 = Entry(master)
        self.e2 = Entry(master)
        self.e3 = Entry(master, textvariable = text, fg = 'red')
        self.e4 = Entry(master)
        self.e5 = Entry(master, textvariable = text, fg = 'red')
        self.e6 = Entry(master)
        self.e7 = Entry(master)

I'm guessing there is something going wrong in def browseFiles(self) .我猜def browseFiles(self)问题。 Any ideas on what's going on?关于发生了什么的任何想法?

You've mixed 2 different strategies.你混合了两种不同的策略。 Either you use configure to set the text attribute on a Label:您可以使用 configure 在 Label 上设置文本属性:

class Initial(simpledialog.Dialog):
    def browseFiles(self): 
        self.e3.configure(text=filename1)

    def body(self, master):
        self.e3 = Label(master, fg = 'red')

Or use the insert command on an Entry:或者在条目上使用插入命令:

class Initial(simpledialog.Dialog):
    def browseFiles(self):
        self.e3.delete('0', tk.END)
        self.e3.insert('0', filename1)

    def body(self, master):
        self.e3 = Entry(master, fg = 'red')

Or you make a StringVar() and set that instead (Entry or Label):或者您创建一个 StringVar() 并设置它(条目或标签):

class Initial(simpledialog.Dialog):
    def browseFiles(self): 
        self.e3_text.set(filename1)

    def body(self, master):
        self.e3_text = tk.StringVar()
        e3 = Entry(master, textvariable=self.e3_text, fg = 'red')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM