簡體   English   中英

Python3和Tkinter:從函數編輯條目

[英]Python3 & Tkinter : Editing a entry from a function

我試圖設置一個輸入對象的文本。 在我的應用程序中,有3個按鈕,當按下它們時,將顯示一個對話框,提供給用戶選擇目錄。 我試圖使用以下代碼來設置Near 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()

如何設置輸入文本?

插入這部分代碼,例如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')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM