【问题标题】:Python3 & Tkinter : Editing a entry from a functionPython3 & Tkinter:从函数编辑条目
【发布时间】:2019-12-25 14:32:57
【问题描述】:

我正在尝试设置条目对象的文本。 在我的应用程序中有 3 个按钮,按下时会显示一个对话框,供用户选择目录。 我尝试使用以下代码来设置附近条目的文本以及对话框返回的路径。 到目前为止,我可以在控制台上打印它,但我无法将字符串分配给条目。

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

如何设置条目文本?

【问题讨论】:

  • @ncica 如果您检查 set_text 功能,您可以理解我尝试遵循该解决方案,但它在我的情况下不起作用
  • 请尝试将您的代码缩减为minimal reproducible example。我认为我们不需要十几个小部件来重现您的问题。另外,请描述为什么调用set_text 不起作用。它会抛出错误吗?如果是这样,错误是什么?

标签: python python-3.x user-interface tkinter


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-27
    • 2021-06-15
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多