tkinter Entry和Radiobutton变量未更新

时间:2019-12-28 19:41:18

标签: python variables tkinter radio-button tkinter-entry

我在PyCharm上使用python 3.8。 我想创建一个GUI,在其中可以选择创建目录或文件并为其命名。 这只是功能

def ask_add():
    global aroot
    aroot = Tk()
    aroot.asking = True
    name_var = StringVar()
    aroot.geometry('500x400')
    aroot.title('OOS')
    aroot.config(bg='black')
    # name label
    name_label = Label(aroot, text='Name:', bg='black', fg='#00ff00', font=16)
    name_label.grid(row=0, column=0, padx=20, pady=10)
    # name entry
    name_entry = Entry(aroot, bg='black', fg='#00ff00', insertbackground='#00ff00', textvariable=name_var, width=40)
    name_entry.grid(row=0, column=1)
    # type label
    type_label = Label(aroot, text='Type:', fg='#00ff00', bg='black', font=16)
    type_label.grid(row=1, column=0)
    # type radio buttons
    type_var = StringVar()
    file_option = Radiobutton(aroot, bg='black', fg='#00ff00', text='File', font=16, variable=type_var, value='File', activebackground='#00ff00', activeforeground='black')
    file_option.grid(row=1, column=1)
    dir_option = Radiobutton(aroot, bg='black', fg='#00ff00', text='Dir', font=16, variable=type_var, value='Dir', activebackground='#00ff00', activeforeground='black')
    dir_option.grid(row=2, column=1)
    # create dir / file
    create_button = Button(aroot, text='Create', bg='black', fg='#00ff00', font=18, activebackground='#00ff00', activeforeground='black', command=lambda: add(name_var.get(), type_var.get()))
    create_button.grid(row=3, column=1)

    while aroot.asking:
        aroot.update()

这是 add()函数

def add(n, t): # name, type
    global aroot
    aroot.asking = False
    aroot.destroy()
    print(n, t)
    if t == 'File':
        p = subprocess.Popen(f'echo.>{n}', shell=True, stderr=subprocess.PIPE)
        err = str(p.stderr.read().decode())
        if err != '':
        tkinter.messagebox.showerror(title='Error', message=err)
    else: # t == Dir
        p = subprocess.Popen(f'md {n}', shell=True, stderr=subprocess.PIPE)
        err = str(p.stderr.read().decode())
        if err != '':
            tkinter.messagebox.showerror(title='Error', message=err)
    update() # updates a window that displays directories

我希望脚本将 name_var type_var 传递给函数 add(),但是这些变量不会更新(当我键入Entry或单击单选按钮时,它们的值仍为''),因此函数 add()无法创建任何文件或目录。 我还尝试在while循环中打印变量

    while aroot.asking:
        print(name_var.get(), type_var.get())
        aroot.update()

,但它们的值仍为''。 有人可以告诉我我在做什么错吗?谢谢

1 个答案:

答案 0 :(得分:0)

每当我使用command=lambda: type_var.set(*my value*)单击单选按钮时,只需更改 type_var 的值即可解决问题。

file_option = Radiobutton(aroot, bg='black', fg='#00ff00', text='File', font=16, variable=type_var, value='File', activebackground='#00ff00', activeforeground='black', command=lambda: type_var.set('File'))
file_option.grid(row=1, column=1)
dir_option = Radiobutton(aroot, bg='black', fg='#00ff00', text='Dir', font=16, variable=type_var, value='Dir', activebackground='#00ff00', activeforeground='black', command=lambda: type_var.set('Dir'))
dir_option.grid(row=2, column=1)
相关问题