python - tkinter (3.8) 中不显示该按钮
问题描述
我对 Python 3.8 有疑问。我正在使用 tkinter。我正在尝试做一个简单的 UI(我正在做一些测试,实际上我正在学习 tkinter)这是代码:
### 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 ###
问题是按钮没有出现,当我关闭应用程序时,控制台会打印出这个东西:
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
解决方案
你在打电话mainloop()
。mainloop 创建一个无限循环,直到您关闭程序。将所有内容放在它之前,否则它将在您关闭应用程序后执行(应用程序已被销毁)。在您的情况下,Tkinter 通过调用其中一个包管理器来初始化它的 UI pack()
。
推荐阅读
- azure - 是否可以使用转换 DNS 后缀的 Bind9 设置 DNS 转发?
- c# - 如何利用用户在 DataContext 列表中添加的信息?
- android - 别名资源文件中当前显示的布局的名称
- python-3.x - MongoDB 存储数据是否比 pandas 读/写 to_excel 更快?
- docker - Dockerized Vue 应用程序 - 我应该使用 docker 还是节点 env 变量?
- react-native - 在 2 个选项卡之间传递数据
- python - 想要检查存储库是否存在以及它是否是公共的(gitpython)
- sql - 无法读取数据集错误的下一个数据
- java - 我应该如何访问片段布局中的自定义视图?
- python - 带有所有星号的python croniter的问题