为什么 get() 方法不返回任何值?

时间:2021-05-07 14:58:02

标签: python file tkinter

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)

我正在做一个便利贴应用。有作为单选按钮列出的便利贴及其文件名。这个想法是,当我选择一个单选按钮并单击读取按钮时,Read_PostIt 函数将读取便利贴文件并显示文本。但是,当 selected_postit.get() 行应该向我返回便利贴的名称时,它不会返回任何内容。 ```print(postit)´´´ 只是打印一个空行。这是为什么?

这是输出和回溯:

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/'

程序应该在其目录路径的末尾添加便利贴的名称,但在末尾没有任何内容。

请有人帮忙。

1 个答案:

答案 0 :(得分:2)

您每次循环都会创建一个新的 StringVar,因此每个单选按钮都与不同的按钮相关联。在循环之后,selected_postit 是与最后一个单选按钮相关联的 StringVar,而不是所有单选按钮。

创建一次 StringVar,并将所有按钮与其关联。

另一个问题是您没有设置单选按钮的 value,因此默认为其在组中的整数序列。

    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)