创建两个按钮方法Python

2024-10-01 09:19:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Python创建一个基于用户输入创建形状的程序。我需要创建两个函数来使用Zeller's graphic.py文件创建按钮。一个按钮需要说退出,第二个按钮需要说进程。以下是到目前为止我所拥有的,但正如您所见,它们不在定义的函数中:

#create Quit button
quitButton = Text(Point(70,73), "Quit")
quitButton.draw(w)
Rectangle(Point(45, 50), Point(95,97)).draw(w)

#create Process button
enterButton = Text(Point(145,73), "Process")
enterButton.draw(w)
Rectangle(Point(120, 48), Point(170,98)).draw(w)

以下是必要方法的说明

  • createButton(text, pt1button, pt2button, win)创建一个角点为pt1button和{}的矩形,文本在windowwin中居中
  • clickedButton(button, clickPt)如果clickPt在按钮中,则返回true/false。在

我试图创建函数,但收到以下错误。在

我的职能是:

^{pr2}$

这里是我调用函数的地方:

createButton("Process",145,73,win) 
createButton("Quit",70,73,win) 

以下是引发的错误:

Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/project4FINAL.p‌​y", line 77, in <module> main()
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/project4FINAL.p‌​y", line 27, in main buttonCreate("Process", 145,73, win)
NameError: global name 'win' is not defined 

有什么建议吗?在


Tags: 函数textcreatebuttonprocess按钮winquit
1条回答
网友
1楼 · 发布于 2024-10-01 09:19:54

因此,从代码来看,您似乎希望为每个按钮创建一个回调函数,然后通过GraphWin方法的setMouseHandler将每个按钮分配给画布。在

因此,从API中给出的示例来看:

from graphics import *

def example_callback():
    print "I love unicorns!" 

def main():
    # win is a Canvas with a setMouseHandler method
    win = GraphWin("My Circle", 100, 100)
    c = Circle(Point(50,50), 10)
    c.draw(win)

    #Add a callback to the canvas
    c.cavas.setMouseHandler(example_callback)
    # or win.setMouseHandler(example_callback)

    win.getMouse() # Pause to view result
    win.close()    # Close window when done

main()

除非在回调中进行了边界检查(以查看鼠标在画布上的哪个形状),否则每个绘制的形状应该只有一个画布。在

一个使用createButton函数的示例:

^{pr2}$

下面是使用新按钮对象的完整示例:

from graphics import *

class Button(object):
    def __init__(self, text, text_pos, rect_pos, win, callback):
        self.win = win
        self.text = Text(text_pos, text)
        self.text.draw(self.win)

        # the limits of the button will be defined by the rectangle                                                              
        self.coords = [rect_pos[0].x,rect_pos[0].y,rect_pos[1].x,rect_pos[1].y]
        self.rect = Rectangle(*rect_pos)
        self.rect.draw(self.win)

        # set the buttons callback
        self.callback = callback

    def _is_inside(self,click):
        limits = self.coords
        return  (limits[0] < click.x < limits[2]) and (limits[1] < click.y < limits[3])

class MyWindow(object):
    def __init__(self,coords=(0,0,100,100)):
        self.win = GraphWin()
        self.win.setCoords(*coords)

        # a list of all possible buttons
        self.buttons = []

        # register the master callback function
        self.win.setMouseHandler(self._callback)
        self._quit = False

        #create a quit and confess button with a custom create method
        self.create_button("Quit",(Point(10,10),Point(40,40)),Point(20,20),self.quit)
        self.create_button("Confess",(Point(50,50),Point(80,80)),Point(60,60),self.confess)

    def create_button(self,text,coords,text_coords,callback):
        button = Button(text,text_coords,coords,self.win,callback)
        # create a button and add it to our list of buttons
        self.buttons.append(button)

    def confess(self,point):
        print
        print "I love unicorns!"
        print

    def quit(self,point):
        self._quit = True
        self.win.close()

    # This function is called for any click on the canvas
    def _callback(self,point):
        # Need to do a coordinate transform here to get the click in the coordinates of the button
        x,y = self.win.trans.world(point.x,point.y)
        point = Point(x,y)
        print "Clicked at x=%d, y=%d"%(point.x,point.y)

        # cycle through buttons and execute all for which the clicked point lies inside their rectangle
        for button in self.buttons:
            if button._is_inside(point):
                button.callback(point)

    # a loop to keep getting mouse input
    def run(self):
        while not self._quit:
            try:
                self.win.getMouse()
            except GraphicsError:
                break

if __name__ == "__main__":
    x = MyWindow()
    x.run()

相关问题 更多 >