I'm using python 3.8 on PyCharm. I want to create a GUI in which I can choose between making a directory or a file and give it a name. Here is just the function
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()
and here is the add() function
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
I expect the script to pass the name_var and the type_var to the function add(), but these variables don't update (their value remains '') when I type in the Entry or click the Radiobuttons, so the function add() cannot create any file or directory. I also tried to print the variables in the while loop
while aroot.asking:
print(name_var.get(), type_var.get())
aroot.update()
but their value remains ''. Can someone tell me what I'm doing wrong? Thanks
Solved my problem just by changing the value of type_var whenever I click a radiobutton using command=lambda: type_var.set(*my value*)
.
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)