I have the following python code with tkinter module. I would like buttons to be an inner function in my code, below is an example where I face the same issue of tkinter button executing itself before I click it
from tkinter import *
from tkinter.filedialog import asksaveasfile
def main():
root = Tk()
root.geometry("777x575")
root.columnconfigure(0, weight=1)
root.columnconfigure(1, weight=2)
root.rowconfigure(0, weight=1)
text = Text(root, width = 405 , height = 205)
text.place(x=500, y=10, anchor=S)
def save():
Files = [('All Files', '*.*'),
('Python Files', '*.py'),
('Text Document', '*.txt')]
file = asksaveasfile(filetypes = Files, defaultextension = Files)
button = Button(root, text = 'Save', command = lambda : save('Save'))
button.place(anchor = CENTER, x = 400, y = 500)
save()
root.mainloop()
if __name__ == '__main__':
main()
I have looked at solutions but nothing really has worked for this case. Any suggestions to solve this issue is highly appreciated.
CodePudding user response:
You called save() function before enters mainloop thats why it shows. Also in button command you called function with a parameter where function doesn't accept any arguments.
.
.
.
text = Text(root, width = 405 , height = 205)
text.place(x=500, y=10, anchor=S)
def save():
Files = [('All Files', '*.*'),
('Python Files', '*.py'),
('Text Document', '*.txt')]
file = asksaveasfile(filetypes = Files, defaultextension = Files)
button = Button(root, text = 'Save', command = lambda : save())
button.place(anchor = CENTER, x = 400, y = 500)
root.mainloop()
this should work