Aatu Tahkola
Aatu Tahkola

Reputation: 35

Why get() method doesn't return any value?

def Main_Menu():
    for widget in myframe_1.winfo_children():
        widget.destroy()
    PostIt_Count = len([name for name in os.listdir('C:/Users/Aatu/Documents/python/pythonleikit/tkinterstuff/PostItApp/PostIts')])
    if PostIt_Count > 0:
        for i in range(PostIt_Count):
            PostIt_NamesList = [name for name in os.listdir('C:/Users/Aatu/Documents/python/pythonleikit/tkinterstuff/PostItApp/PostIts')]
            global selected_postit
            selected_postit = tk.StringVar()
            PostIt_ButtonName = ttk.Radiobutton(myframe_1, text=PostIt_NamesList[i], variable=selected_postit)
            y = ([x for x in range(1, PostIt_Count +1)][i])- 0.4
            if PostIt_Count < 10:
                y = str(y)[:1] + str(y)[2:]
            else:
                y = str(y)[:2] + str(y)[3:]
            yname = '.' + y
            PostIt_ButtonName.place(relx=.1, rely=yname)

def Read_PostIt():
    for widget in myframe_3.winfo_children():
        widget.destroy()
    filepath = 'C:/Users/Aatu/Documents/python/pythonleikit/tkinterstuff/PostItApp/PostIts/'
    postit = selected_postit.get()
    print(postit)
    f = filepath + postit
    print(f)
    os.chmod('C:/Users/Aatu/Documents/python/pythonleikit/tkinterstuff/PostItApp/PostIts/', stat.S_IRWXO)
    with open('{}'.format(f), 'r') as fi:
        global text
        text = fi.readlines()
        fi.close()
    text_label = Label(myframe_3, text='{}'.format(text))
    text_label.place(relx=.01, rely=.01)

I'm doing a Post-It app. There are post-its listed as radiobuttons with the name of their filenames. The idea is that when I select a radiobutton and click Read-button, the Read_PostIt function would read the post-it file and show the text. However, when the selected_postit.get() line should return me the name of the post-it, it doesn't return anything. ```print(postit)´´´ just prints an empty line. Why is this?

Here is the output and traceback:

PS C:\Users\Aatu\Documents\python\pythonleikit> & C:/Python39ni/python.exe c:/Users/Aatu/Documents/python/pythonleikit/tkinterstuff/PostItApp/post-its.py

C:/Users/Aatu/Documents/python/pythonleikit/tkinterstuff/PostItApp/PostIts/
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python39ni\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "c:\Users\Aatu\Documents\python\pythonleikit\tkinterstuff\PostItApp\post-its.py", line 77, in Read_PostIt
    with open('{}'.format(f), 'r') as fi:
PermissionError: [Errno 13] Permission denied: 'C:/Users/Aatu/Documents/python/pythonleikit/tkinterstuff/PostItApp/PostIts/'

The program should add the name of the post-it at the end of its directory path but nothing appears at the end.

Someone pls help.

Upvotes: 0

Views: 56

Answers (1)

Barmar
Barmar

Reputation: 780673

You're creating a new StringVar each time through the loop, so each radio button is tied to a different one. After the loop, selected_postit is the StringVar associated with the last radio button, not all of them.

Create the StringVar once, and associate all the buttons with it.

Another problem is that you didn't set the value of the radio buttons, so it's defaulting to its integer sequence in the group.

    if PostIt_Count > 0:
        global selected_postit
        selected_postit = tk.StringVar()
        for i in range(PostIt_Count):
            PostIt_NamesList = [name for name in os.listdir('C:/Users/Aatu/Documents/python/pythonleikit/tkinterstuff/PostItApp/PostIts')]
            PostIt_ButtonName = ttk.Radiobutton(myframe_1, text=PostIt_NamesList[i], value=PostIt_NamesList[i], variable=selected_postit)
            y = ([x for x in range(1, PostIt_Count +1)][i])- 0.4
            if PostIt_Count < 10:
                y = str(y)[:1] + str(y)[2:]
            else:
                y = str(y)[:2] + str(y)[3:]
            yname = '.' + y
            PostIt_ButtonName.place(relx=.1, rely=yname)

Upvotes: 2

Related Questions