【发布时间】:2017-05-02 18:23:45
【问题描述】:
我是编程新手,这是我在网站上的第一篇文章。我确定我犯了一个愚蠢的错误,但我真的很感激朝着正确的方向前进。我正在尝试制作一个计算器,并希望制作一个为数字生成 Button 对象的函数。当我尝试运行它时,我得到了错误:
'NameError: name 'num_but_gen' is not defined'
代码如下:
from tkinter import *
WINDOW_HEIGHT = 300
WINDOW_WIDTH = 325
class Window(Frame):
def __init__(self, master = None):
Frame.__init__(self, master)
self.master = master
self.init_window()
def num_but_gen(self, disp, xloc=0, yloc=0, wid=0, hei=0):
self.Button(text='{}'.format(disp),height=hei, width=wid)
self.place(x=xloc, y=yloc)
def init_window(self):
self.master.title('Calculator')
self.pack(fill=BOTH, expand=1)
Button1 = num_but_gen('1', xloc=0, yloc=200, wid=40, hei=40)
root = Tk()
app = Window(root)
root.geometry("{}x{}".format(WINDOW_WIDTH,WINDOW_HEIGHT))
root.mainloop()
任何帮助将不胜感激!还可以向任何对如何在以后的帖子中更好地表达我的问题标题提出建议的人加分。
【问题讨论】:
-
这是你类的方法。您必须将其称为
self.num_but_gen(...),就像您对init_window()所做的那样。
标签: python-3.x class tkinter