首页 > 解决方案 > Python3 & Tkinter : Editing a entry from a function

问题描述

Im trying to set the text of a entry object. In my app there are 3 button that when pressed show a dialogbox that provide to the user to select a directory . Im trying with the following code to set the text of the near Entry with the path that the dialogbox return. Till now I was able to print it on the console but I can't assign the string to the entry.

from tkinter import *
from tkinter import filedialog
from tkinter import messagebox




class MiaApp:
    def __init__(self, genitore):

        self.mioGenitore = genitore


        def set_text(entry_obj,text):
            entry_obj.delete(0,END)
            entry_obj.insert(0,text)
            return




        def open_filetoupload():
            upload_text = filedialog.askdirectory(parent=genitore, initialdir='/')
            set_text(self.file_da_caricare,upload_text)
            print(upload_text)

        def open_filelog():
            log_text = filedialog.askdirectory(parent=genitore, initialdir='/')
            set_text(self.file_log,log_text)
            print(log_text)

        def open_archive():
            archive_text = filedialog.askdirectory(parent=genitore, initialdir='/')

            set_text(self.file_archive,archive_text)
            print(self.file_archive.text)





        self.file_da_caricare = Entry( width =30, state='disabled')
        self.file_log = Entry( width = 30 , state='disabled')
        self.file_archive = Entry( width =30, state='disabled')

        self.file_da_caricare_label = Label( text="File da caricare" )
        self.file_da_caricare_label.grid(row =1 , column= 1 ,pady= 10,sticky="W")


        self.file_da_caricare.grid(row =1 , column= 2 ,pady= 10)

        self.file_da_caricare_button = Button(text='...',command = open_filetoupload)
        self.file_da_caricare_button.grid(row =1 , column= 3 ,pady= 10,sticky="W" )

        self.file_log_label = Label( text="File di log" )
        self.file_log_label.grid(row =2 , column= 1 ,pady= 10,sticky="W")


        self.file_log.grid(row =2 , column= 2 ,pady= 10)

        self.file_log_button = Button(text='...',command = open_filelog)
        self.file_log_button.grid(row =2 , column= 3 ,pady= 10,sticky="W")

        self.file_archive_label = Label( text="Archivio" )
        self.file_archive_label.grid(row =3 , column= 1 ,pady= 10,sticky="W")

        self.file_archive = Entry( width =30)
        self.file_archive.grid(row =3 , column= 2 ,pady= 10)

        self.file_archive_button = Button(text='...' ,command = open_archive)
        self.file_archive_button.grid(row =3 , column= 3 ,pady= 10,sticky="W")

        self.scelta_test = Radiobutton(self.mioGenitore, text="Test", value='TEST')
        self.scelta_test.grid(row =4 , column= 3 ,pady= 10)

        self.scelta_prod = Radiobutton(self.mioGenitore, text="Prod",  value='PROD')
        self.scelta_prod.grid(row =5 , column= 3 ,pady= 10)


        self.username_label = Label( text="Username" )
        self.username_label.grid(row =6 , column= 1 ,pady= 10,sticky="W")


        self.username_field = Entry( width =15)
        self.username_field.grid(row =6 , column= 2 ,pady= 10,sticky="W")

        self.exit_button = Button(text='Esci',command =genitore.destroy)
        self.exit_button.grid(row =6 , column= 3 ,pady= 10,sticky="W")

        self.password_label = Label( text="Password" )
        self.password_label.grid(row =7, column= 1 ,pady= 10,sticky="W")

        self.password_field = Entry( width =15)
        self.password_field.grid(row =7 , column= 2 ,pady= 10,sticky="W")

        self.send_button = Button(text='Invia',bg='blue')
        self.send_button.grid(row =7 , column= 3 ,pady= 10,sticky="W")

        self.textArea = Text(height=2, width=30)
        self.textArea.grid(row =8 , column= 1, columnspan=1)



radice = Tk()
radice.title("DataLoader")
radice.geometry("700x400")
miaApp = MiaApp(radice)
radice.mainloop()

How can it possible to set the entry text ?

标签: pythonpython-3.xuser-interfacetkinter

解决方案


插入这部分代码,例如self.file_da_caricare

   def open_filetoupload():
        upload_text = filedialog.askdirectory(parent=genitore, initialdir='/')
        #set_text(self.file_da_caricare,upload_text)
        print(upload_text)
        self.entryText.set(upload_text)

    self.entryText = tk.StringVar()
    self.file_da_caricare = Entry( width =30, textvariable=self.entryText, state='disabled')

推荐阅读