-1

I have a problem with Python 3.8. I am using tkinter. I was trying to do a simple UI (I was doing some tests, actually I am learning tkinter) Here is the code:

### importing libraries ###
import tkinter 
from tkinter import *
import os, glob
### importing libraries ###

### creating tk and the principal frame ###
tk = Tk()
tk.title("calculator")
 f1 = Frame(tk, bg="white", height=1200, width=1200)
 f1.pack()
 #### creating tk and the principal frame ####

 #### initializing the ui ####
 f1.mainloop()
 #### initializing the ui####

### creating buttons ###
bruh = tkinter.Button(f1,background="white", text="moltiplica")
### creating buttons ###

### adding buttons and setting the position ###
bruh.pack(padx=30,pady=1)
### adding buttons and setting the position ###

the problem is that the button doesn't appears and when i close the application the console prints out this thing:

File "c:/Users/MS/### importazione delle librerie ###.py", line 21, in <module>bruh = tkinter.Button(f1,background="white",  command="ciao", text="moltiplica")File "C:\Users\MS\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 2645, in __init_ Widget.__init__(self, master, 'button', cnf, kw)File "C:\Users\MS\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 2567, in __init__self.tk.call(
_tkinter.TclError: can't invoke "button" command: application has been destroyed
Banana
  • 2,295
  • 1
  • 8
  • 25
  • 3
    Does this answer your question? [Tkinter understanding mainloop](https://stackoverflow.com/questions/29158220/tkinter-understanding-mainloop) – tgikal Mar 30 '20 at 14:23
  • Thank you all! I solved the problem!(thanks to Just learnedit) –  Mar 31 '20 at 07:03

1 Answers1

1

You're calling mainloop(). mainloop creates an infinite loop until you close the program. Put everything before it or it will be executed after you closed your app (application has been destroyed). Tkinter initializes it's UI by calling one of the package manager, in your case pack().

Banana
  • 2,295
  • 1
  • 8
  • 25