【问题标题】:how do i make tkinter update我如何使 tkinter 更新
【发布时间】:2022-11-29 16:59:40
【问题描述】:

我有一个将我的列表存储到文本文件的输入字段 当我按下按钮存储信息时,它被存储但我必须重新启动应用程序才能在选项菜单上看到它 如何在不重启的情况下更新应用程序? `

from tkinter import *
from tkinter import messagebox

root = Tk()
root.title("test tool") #App Title
root.iconbitmap("D:\\Software\\GigaPixel Frames\\Dump\\New folder\\imgs\\Logo.ico")
root.geometry("1600x800") #App Dimensions

DropDownvar = StringVar(value="Select an option")
DropDownvar.set("Select an option")

my_list = open("Characters.txt").readlines()
DropDownMenu = OptionMenu(root, DropDownvar, *my_list)
DropDownMenu.pack()

inputBox = Entry(root)
inputBox.pack()

def ButtonFun():
  InputBoxEntry = inputBox.get()
  with open("Characters.txt", "a") as text_file:
      text_file.write(InputBoxEntry + "\n")
  root.update()
inputBoxButton = Button(root, text="Input", command=ButtonFun)
inputBoxButton.pack()







root.mainloop()

`

找不到答案

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    在输入和添加行后,您应该重新阅读并更新您的列表。您应该避免使用 my_list = open("Characters.txt"),因为您可能会忘记关闭它。或者有时它会给出错误并保持未关闭状态,您无法对其执行任何操作。

    from tkinter import *
    from tkinter import messagebox
    
    root = Tk()
    root.title("test tool") #App Title
    root.geometry("1600x800") #App Dimensions
    
    DropDownvar = StringVar(value="Select an option")
    DropDownvar.set("Select an option")
    
    
    DropDownMenu = OptionMenu(root, DropDownvar," ")
    DropDownMenu.pack()
    
    inputBox = Entry(root)
    inputBox.pack()
    
    def fillOptionMenu():
        with open("characters.txt","r") as f:
            my_list = f.readlines()
    
        DropDownMenu["menu"].delete(0,"end")
        # DropDownMenu.set_menu(my_list)
        for i in my_list:
            DropDownMenu["menu"].add_command(label=i, command=lambda value=i: DropDownvar.set(i))
    
    def ButtonFun():
        InputBoxEntry = inputBox.get()
        with open("Characters.txt", "a") as text_file:
            text_file.write(InputBoxEntry + "
    ")
        root.update()
    
        fillOptionMenu()
    
    
    inputBoxButton = Button(root, text="Input", command=ButtonFun)
    inputBoxButton.pack()
    
    fillOptionMenu()
    
    root.mainloop()
    

    【讨论】:

      【解决方案2】:

      您需要在ButtonFun() 中手动将输入项添加到下拉列表中。

      同样使用 .readlines() 不会从文件中的每一行中删除尾随的换行符 ' ',最好使用 .read().splitlines() 代替:

      ...
      with open("Characters.txt") as f:
          my_list = f.read().splitlines()
      DropDownMenu = OptionMenu(root, DropDownvar, *my_list)
      DropDownMenu.pack()
      ...
      def ButtonFun():
        InputBoxEntry = inputBox.get()
        with open("Characters.txt", "a") as text_file:
            text_file.write(InputBoxEntry + "
      ")
        # add the input item to dropdown
        DropDownMenu["menu"].add_command(label=InputBoxEntry, command=lambda: DropDownvar.set(InputBoxEntry))
      ...
      

      【讨论】:

        猜你喜欢
        • 2014-02-10
        • 1970-01-01
        • 1970-01-01
        • 2015-12-08
        • 1970-01-01
        • 2018-05-06
        • 1970-01-01
        • 1970-01-01
        • 2022-01-08
        相关资源
        最近更新 更多