首页 > 解决方案 > 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 位置时,按钮就不会出现。

我错过了什么?

标签: pythontkinterbutton

解决方案


你忘记传递self.masterButton()内部ncr_field.button()函数,所以它应该是:

def button(self, text, command):
    self.widget = Button(self.master, text=text, command=command)

推荐阅读