python - Tkinter 按钮未出现在框架上
问题描述
由于某种原因,我的代码中的按钮不会出现在页面上:
def roto_peen():
Z = StringVar()
Z.set("-")
def roto_calc():
x = float(first_strip.get())
y = float(second_strip.get())
Z.set((x - y) / x * .77)
result.config(text=f"{Z}")
roto = ncr_field(root)
roto.toplevel()
roto.set_title("P")
roto.set_geometry("400x300")
roto_frame = ncr_field(roto.widget)
roto_frame.labelframe(width=250, height=60, text='')
roto_frame.set_grid(row=0, col=0, columnspan=3, padx=20, pady=6)
roto_frame.widget.grid_propagate(False)
ncr_fields.append(roto_frame)
first_strip = ncr_field(roto_frame.widget)
first_strip.entry(hint_text="First Strip", font=("Helvetica", 12), width=20)
first_strip.set_grid(row=0, col=0, padx=20)
ncr_fields.append(first_strip)
second_strip = ncr_field(roto_frame.widget)
second_strip.entry(hint_text="Second Strip", font=("Helvetica", 12), width=20)
second_strip.set_grid(row=1, col=0, padx=20)
ncr_fields.append(second_strip)
result = ncr_field(roto.widget)
result.label(textvariable=Z, text='')
result.set_grid(col= 1, row=1)
submit = ncr_field(roto.widget)
submit.button(text="Calculate", command=roto_calc)
submit.set_grid(row=2, col=0, padx=20)
ncr_fields.append(submit_peen)
在我的导入文件上:
def button(self, text, command):
self.widget = Button(text=text, command=command)
def set_grid(self, row, col, **kw):
self.widget.grid(row=row, column=col, **kw)
def set_geometry(self, geometry):
self.widget.geometry(geometry)
def set_title(self, title):
self.widget.title(title)
enter code here
def label(self, text, textvariable):
self.widget = Label(self.master, height=1, text=text, textvariable=textvariable)
def labelframe(self, text, width, height, **kwargs) :
self.widget = LabelFrame(self.master, width=width, height=height, text=text, **kwargs)
希望我得到了所有相关的信息,但是.. 当我打开页面时,当明显它有一个 .grid 位置时,按钮就不会出现。
我错过了什么?
解决方案
你忘记传递self.master
给Button()
内部ncr_field.button()
函数,所以它应该是:
def button(self, text, command):
self.widget = Button(self.master, text=text, command=command)
推荐阅读
- java - Spring maven 应用程序在应用程序上下文加载后立即终止
- android - 如何将我 PC 上的所有 Android Studio 项目提交到私有 GitHub 存储库?
- powershell - IF 两次 powershell 之间
- angular - Angular - 如何确定 FormArray 中的新项目?
- html - Bootstrap 4 移动 flexbox 太宽了
- java - Json的Java对象无法使用Jackson Object Mapper工作
- php - php json_encode() 效果很好。但是,结果在客户端被破坏
- android - Android - 更改文本颜色
- php - 我的注册页面没有检查数据库以查看是否已使用用户名
- javascript - 具有异常数据的 d3 堆积条形图