首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >更新tkinter画布上的文本字段

更新tkinter画布上的文本字段
EN

Stack Overflow用户
提问于 2014-12-17 22:46:41
回答 1查看 2.7K关注 0票数 0

我第一次用Python中的tkinter创建了一个GUI。我曾与wxPython合作过,但我从未使用过画布。在画布背后的思想过程是最终添加动画。我创建了一个网格布局,其中一个网格是画布。我试图使用itemconfig更新一个得分字段,但没有运气,可以在画布上更新两个得分字段。我的密码在下面。对于如何更新画布上的两个分数,有什么建议吗?我希望旧的分数被删除之前,新的分数写到屏幕上。谢谢你看一看。原谅全球,他们将被移除,因为这一进程。

代码语言:javascript
运行
复制
class Gui(Frame):
def __init__(self, root):
    self.root=root
    self.initUI()

def initUI(self):
    global score1, score2   # these are ints
    score1str = str(score1)
    score2str = str(score2)

    root.title("Table Tennis Scoreboard")

    self.canvas = Canvas(root, width=640, height=480, background='green')
    self.canvas.grid(row=0,column=1)

    self.canvas.create_text(185, 90, anchor=W, font=('Calibri', 20),
                                             text="Let's play Table Tennis!")

    self.canvas.create_rectangle(75, 120, 275, 320, outline="red", fill="red")
    self.canvas.create_rectangle(370, 120, 565, 320, outline="blue", fill="blue")

    self.canvas.create_text(125, 130, anchor=W, font=('Calibri', 20),
                                             text="Player 1")
    self.canvas.create_text(425, 130, anchor=W, font=('Calibri', 20),
                                             text="Player 2")

    self.score1_id = self.canvas.create_text(135, 250, anchor=W, font=('Arial', 100),
                                             text=score1str)
    self.score2_id = self.canvas.create_text(435, 250, anchor=W, font=('Arial', 100),
                                             text=score2str)

    frame = Frame(self.root, relief=RAISED, borderwidth=2, background='white')
    frame.grid(row=0,column=0)

    p1up = Button(frame,text="Player1 +1", height=2, width=10, background='red', command=self.add('one'))
    p1up.grid(row = 0,column = 0, padx=10, pady=(20,5))
    p1down = Button(frame,text="Player1 -1", height=2, width=10, background='red', command=self.subtract('one'))
    p1down.grid(row = 1,column = 0, padx=10, pady=(5,100))
    p2up = Button(frame,text="Player2 +1", height=2, width=10, background='blue', command=self.add('two'))
    p2up.grid(row = 3,column = 0, padx=10, pady=5)
    p2down = Button(frame,text="Player2 -1", height=2, width=10, background='blue', command=self.subtract('two'))
    p2down.grid(row = 4,column = 0, padx=10, pady=(5,100))
    reset = Button(frame,text="Reset", height=2, width=10, command=self.new_game())
    reset.grid(row = 6,column = 0, padx=10, pady=(5,20))

def new_game(self):
    global score1, score2   # these are ints
    score1 = 0
    score2 = 0
    self.update_score()

def add(self, player):
    global score1, score2  # these are ints
    self.player = player
    if self.player == 'one':
        #if score1 < 11:
        score1 += 1
    elif self.player == 'two':
        #if score2 < 11:
        score2 += 1
    self.update_score()
    return

def subtract(self, player):
    global score1, score2  # these are ints
    self.player = player
    if self.player == 'one':
        if score1 > 0:
            score1 = score1 - 1
    elif self.player == 'two':
        if score2 > 0:
            score2 -= 1
    self.update_score()
    return

def update_score(self):
    global score1, score2   # these are ints

    score1str = str(score1)
    score2str = str(score2)

    self.canvas.itemconfig(self.score1_id, text=score1str)
    self.canvas.itemconfig(self.score2_id, text=score2str)

    return

if __name__== '__main__':
    root=Tk()
    gui=Gui(root)
    root.mainloop()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-18 11:02:51

问题是,您不是将函数分配给按钮,而是返回函数调用的值。例如:

代码语言:javascript
运行
复制
  reset = Button(frame,text="Reset", height=2, width=10, command=self.new_game())

None分配给命令,因为这是self.new_game()返回的内容。您应该将此更改为:

代码语言:javascript
运行
复制
  reset = Button(frame,text="Reset", height=2, width=10, command=self.new_game)

它将函数self.new_game分配给按钮的命令,而不是None

然后,对于其他按钮,要将参数传递给按钮的命令,您应该使用lambda函数,如下所示:

代码语言:javascript
运行
复制
 p1up = Button(frame,text="Player1 +1", height=2, width=10, background='red', command=lambda: self.add('one'))

这将分配一个匿名函数,该函数对按钮的逗号调用self.add('one')

另见:Why is Button parameter "command" executed when declared?

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27536267

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档