Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

My tkinter app wont change the file it is loading

$
0
0

I am writing a tkinter app in python that is a flash card quiz. It needs to be able to read from a txt file one folder ahead from the python file. In the cide you can change which file is the file loaded but this is not working even if you choose the same file. When i tried to change the file in filehandling() it broke but when it launched it worked I tried it with keywords.txt which it was already using and test.txt.

This is the file structure of the code:

This is the file structure of the code

from customtkinter import *from CTkMessagebox import CTkMessageboximport globimport osinformation = []file_name = 'keywords.txt'app = Nonedef hide(tk):    # hides window    try:        tk.withdraw()    except:        passdef show(tk):    # shows window    tk.deiconify()def FileHandling(filename):    # file handling    information = []    with open("Text/"+filename, 'r') as f:        temp = f.read().split("\n")    for i in range(0, len(temp), 2):        temp_list = []        # makes 2d array        temp_list.append(temp[i].capitalize())        temp_list.append(temp[i+1].capitalize())        information.append(temp_list)    return informationdef changeHandler(value):    global file_name    file_name = value    print(value)    return file_namedef changeFile():    # add change file code here    files = getFiles()    filePicker(files)def getFiles():    temp = []    os.chdir("../Code/Text")    for file in glob.glob("*.txt"):        temp.append(file)    return tempdef filePicker(files):    hide(app)    FilePicker = CTkToplevel()    FilePicker.geometry("500x400")    FilePicker.resizable(False, False)    FilePicker.title("FIle Picker")    Title = CTkLabel(        master=FilePicker, text="Which file would you like to load?", font=("Arial", 10))    Title.place(relx=0.5, rely=0.05, anchor="center")    Options = CTkComboBox(FilePicker, values=files,                          variable="Unselected", command=changeHandler)  # type: ignore    Options.place(relx=0.5, rely=0.5, anchor="center")    ExitBtn = CTkButton(master=FilePicker, text="Menu",                        fg_color="#550000", text_color="#000000", command=lambda: Menu(FilePicker)).place(relx=0.5, rely=0.7, anchor="center")def Menu(tk=None):    global information    information = FileHandling(file_name)    global app    # makes app unless app is made then it reveals it.    if app == None:        app = CTk()        app.geometry("500x400+750+300")        app.resizable(False, False)        app.title("Menu")        set_appearance_mode("dark")        Title = CTkLabel(master=app, text="Flash Card Project",                         font=("Arial", 40))        PlayBtn = CTkButton(master=app, text="Start",                            fg_color="#005500", text_color="#000000", command=lambda: Run(app))        Changebtn = CTkButton(master=app, text="Change File",                              fg_color="#AA4203", text_color="#000000", command=changeFile)        ExitBtn = CTkButton(master=app, text="Exit",                            fg_color="#550000", text_color="#000000", command=app.quit)        Title.place(relx=0.5, rely=0.05, anchor="center")        PlayBtn.place(relx=0.5, rely=0.4, anchor="center")        Changebtn.place(relx=0.5, rely=0.5, anchor="center")        ExitBtn.place(relx=0.5, rely=0.6, anchor="center")        app.mainloop()        exit()    else:        show(app)        try:            tk.destroy()        except:            passMenu()
keywords.txtException in Tkinter callbackTraceback (most recent call last):  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1921, in __call__    return self.func(*args)  File "C:\Users\jacen\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\customtkinter\windows\widgets\ctk_button.py", line 554, in _clicked    self._command()  File "E:\Documents\Python Code\Flash Cards Project\Code\test.py", line 75, in <lambda>    fg_color="#550000", text_color="#000000", command=lambda: Menu(FilePicker)).place(relx=0.5, rely=0.7, anchor="center")      File "E:\Documents\Python Code\Flash Cards Project\Code\test.py", line 80, in Menu    information = FileHandling(file_name)  File "E:\Documents\Python Code\Flash Cards Project\Code\test.py", line 27, in FileHandling    with open("Text/"+filename, 'r') as f:FileNotFoundError: [Errno 2] No such file or directory: 'Text/keywords.txt'test.txtException in Tkinter callbackTraceback (most recent call last):  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1921, in __call__    return self.func(*args)  File "C:\Users\jacen\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\customtkinter\windows\widgets\ctk_button.py", line 554, in _clicked    self._command()  File "E:\Documents\Python Code\Flash Cards Project\Code\test.py", line 75, in <lambda>    fg_color="#550000", text_color="#000000", command=lambda: Menu(FilePicker)).place(relx=0.5, rely=0.7, anchor="center")      File "E:\Documents\Python Code\Flash Cards Project\Code\test.py", line 80, in Menu    information = FileHandling(file_name)  File "E:\Documents\Python Code\Flash Cards Project\Code\test.py", line 27, in FileHandling    with open("Text/"+filename, 'r') as f:FileNotFoundError: [Errno 2] No such file or directory: 'Text/test.txt'

keywords.txt

celluloseTough substance that makes up the cell walls of green plants.respirationA chemical reaction that causes energy to be released from glucose.haemoglobinA substance which joins to oxygen and carries it round the body in blood.ventilationBreathing.cartilageTough and smooth substance covering the ends of bones to protect them.cytoplasmJelly-like part of a cell where chemical reactions happen.nucleusControls what happens inside a cell.alveoliTiny air sacs in the lungs.amino acidsProduced when proteins are digested.virusThe smallest type of microbe.white blood cellsCan engulf bacteria or make antibodies.photosynthesisThe process of turning carbon dioxide water and light into glucose and oxygen.stomataSmall holes in the underside of a leaf.vaccineDead or inactive forms of a microorganism.fibreA nutrient that cannot be digested.

test.txt

kTest1Test1kTest2Test2kTest3Test3kTest4Test4kTest5Test5kTest6Test6

Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>