Home > database >  how do i make tkinter update
how do i make tkinter update

Time:11-29

I have an entry field that stores my list to a text file when i press the button to store the info, it gets stored but i have to restart the app to see it on the options menu How do i make the app update without having to restart it? `

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

`

could not find answer

CodePudding user response:

You should re-read and update your list after you put input and added line. You should avoid using my_list = open("Characters.txt") as you may forget to close it. Or sometimes it gives an error and stay unclosed which you cannot perform anything over it.

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   "\n")
    root.update()

    fillOptionMenu()


inputBoxButton = Button(root, text="Input", command=ButtonFun)
inputBoxButton.pack()

fillOptionMenu()

root.mainloop()

CodePudding user response:

You need to add the input item to the dropdown manually inside ButtonFun().

Also using .readlines() will not strip out the trailing newline '\n' from each line in the file, better use .read().splitlines() instead:

...
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   "\n")
  # add the input item to dropdown
  DropDownMenu["menu"].add_command(label=InputBoxEntry, command=lambda: DropDownvar.set(InputBoxEntry))
...
  • Related