Tkinter按钮未注册点击

时间:2018-05-30 14:41:53

标签: python tkinter

我有以下tkinter按钮类

import tkinter as tk

class Button(GraphicsObject):

"""A button is a labeled rectangle in a window.
It is activated or deactivated with the activate()
and deactivate() methods. The clicked(p) method
returns true if the button is active and p is inside it."""

def __init__(self, p, width, height, label):
    """ Creates a rectangular button, eg: qb = Button(myWin, Point(30,25), 20, 10, 'Quit') """

    GraphicsObject.__init__(self, [])
    self.anchor = p.clone()
    w,h = width/2.0, height/2.0
    self.x, self.y = p.getX(), p.getY()
    self.xmax, self.xmin = self.x+w, self.x-w
    self.ymax, self.ymin = self.y+h, self.y-h
    p1 = Point(self.xmin, self.ymin)
    p2 = Point(self.xmax, self.ymax)
    self.width = width
    self.height = height
    self.rect = Rectangle(p1,p2)
    self.label = label
    self.fill = "white"
    self.color = "black"
    self.font = DEFAULT_CONFIG['font']
    self.activate()

def _draw(self, canvas, options):
    p = self.anchor
    x,y = canvas.toScreen(p.x,p.y)
    #frm = tk.Frame(canvas.master,height = self.height, width = self.width,)
   # frm.pack_propagate(0)
    #frm.pack()
    self.button = tk.Button(canvas.master,
                          height = self.height,
                          width = self.width,
                          text = self.label,
                          bg = self.fill,
                          fg = self.color,
                          font=self.font)
    self.button.place(x = self.x, y = self.y, height =self.height, width = self.width)
    #self.setFill(self.fill)
    self.button.focus_set()
    #return canvas.create_window(x,y,window=self.button)

def clicked(self, p):
    """ RETURNS true if button active and p is inside"""
    return self.active and \
           self.xmin <= p.getX() <= self.xmax and \
           self.ymin <= p.getY() <= self.ymax

def getLabel(self):
    """RETURNS the label string of this button."""
    return self.label.getText()

def setColor(self, color):
    self.color = color

def setFill(self, color):
    self.fill = color

def activate(self):
    """Sets this button to 'active'."""
    self.color = 'black'
    self.rect.setWidth(2)
    self.active = 1

def deactivate(self):
    """Sets this button to 'inactive'."""
    self.color = 'darkgrey'
    self.rect.setWidth(1)
    self.active = 0

我首先决定通过调用以下循环中单击的方法来测试它:

sign_in_button = Button(Point(237.5,300),80,40,'Sign In')
sign_in_button.draw(login_page)
click_point = login_page.getMouse()
clicked = sign_in_button.clicked(click_point)
print(clicked)
while(clicked == False):
    click_point = login_page.getMouse()
    clicked = sign_in_button.clicked(click_point)
    print(clicked)
print("clicked == True")

当我运行程序时,按钮在窗口上正确显示,只要我没有在按钮内单击,它就会记录一个单击的值False。但是,当我点击按钮时,没有任何反应。点击时,print(clicked)print(clicked == True)都不会运行,其值应为True。我是否需要修改我的课程或者在尝试创建Button时忘记包含某些内容?

0 个答案:

没有答案