python - Tkinter 代码未显示按钮 (v:3.6)
问题描述
这是我在 youtube 上的练习,它没有在窗口中显示按钮
import tkinter as tk
class Window(Frame):
def _init_ (self, master =None):
Frame.__init__(self, master)
self.master = master
self.init_window()
def init_window(self):
self.master.title('GUI')
self.pack(fill = BOTH, expand = 1)
quitButton = Button(self, text = "Quit")
quitButton.place(x=0, y =.1)
root = tk.Tk()
root.title('GUI')
root.geometry()
app = Window(master=root)
root.mainloop()
解决方案
只需将您更改_init_
为__init__
. Python 构造函数语法有双下划线。
__init__
是 python 类中的一种保留方法。它在面向对象的概念中被称为构造函数。当从类创建对象时调用此方法,它允许类初始化类的属性。
推荐阅读
- java - Java Serial read \u0002 怎么去掉?
- ios - WhatsApp 网络客户端如何仍能与最新的 iOS 更新(SDK 版本 13.0+)一起使用?
- html - 如果span里面的句子太长,不显示句子,显示点
- c# - 如何在 DataGridView.Columns.Count 和列 data.TQ 后面添加多个 TextBox
- python - Python - 使用 beautifulsoup 解析输入 html 数据并将输出数据按列存储在 csv 文件中
- javascript - 单击有源过滤器的选项不起作用
- html - 是否可以通过调整 URL 地址来过滤数据?
- mysql - sql查询有入口但没有出口的记录
- java - Javers @diffIgnore 在类/属性级别不使用继承
- ios - 如何选择 SQLite 中两个值之间的所有行?