Python 3.x 更新tkinter画布上的文本字段

Python 3.x 更新tkinter画布上的文本字段,python-3.x,tkinter,tkinter-canvas,Python 3.x,Tkinter,Tkinter Canvas,我第一次在Python中使用tkinter创建GUI。我曾与wxPython合作过,但没有遇到过同样的问题,但我从未使用过画布。画布背后的思维过程是最终添加动画。我创建了一个网格布局,其中一个网格是画布。我试图使用itemconfig更新一个分数字段,但没有在画布上更新这两个分数字段。我的代码如下。关于如何更新画布上的两个分数,有什么建议吗?我希望在将新分数写入屏幕之前删除旧分数。谢谢你看。原谅地球人,随着这一进程,他们将被移除 class Gui(Frame): def __init__(se

我第一次在Python中使用tkinter创建GUI。我曾与wxPython合作过,但没有遇到过同样的问题,但我从未使用过画布。画布背后的思维过程是最终添加动画。我创建了一个网格布局,其中一个网格是画布。我试图使用itemconfig更新一个分数字段,但没有在画布上更新这两个分数字段。我的代码如下。关于如何更新画布上的两个分数,有什么建议吗?我希望在将新分数写入屏幕之前删除旧分数。谢谢你看。原谅地球人,随着这一进程,他们将被移除

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()
类Gui(框架):
定义初始化(自,根):
self.root=根
self.initUI()
def initUI(self):
全球得分1,得分2#这些是整数
score1str=str(score1)
score2str=str(score2)
根标题(“乒乓球记分板”)
self.canvas=canvas(根,宽度=640,高度=480,背景为绿色)
self.canvas.grid(行=0,列=1)
self.canvas.create_text(185,90,anchor=W,font=('Calibri',20),
text=“让我们打乒乓球吧!”)
self.canvas.create_矩形(75120275320,outline=“red”,fill=“red”)
self.canvas.create_矩形(370120565320,outline=“blue”,fill=“blue”)
self.canvas.create_text(125130,anchor=W,font=('Calibri',20),
text=“播放器1”)
self.canvas.create_text(425130,anchor=W,font=('Calibri',20),
text=“玩家2”)
self.score1_id=self.canvas.create_文本(135250,anchor=W,font=('Arial',100),
text=score1str)
self.score2_id=self.canvas.create_文本(435250,anchor=W,font=('Arial',100),
text=score2str)
frame=frame(self.root,relief=凸起,borderwidth=2,background='white')
frame.grid(行=0,列=0)
p1up=按钮(框架,text=“Player1+1”,高度=2,宽度=10,背景为红色,命令为self.add('one'))
网格(行=0,列=0,padx=10,pady=(20,5))
p1down=按钮(frame,text=“Player1-1”,高度=2,宽度=10,背景=red,命令=self.subtract('one'))
p1down.grid(行=1,列=0,padx=10,pady=(5100))
p2up=按钮(框,text=“Player2+1”,高度=2,宽度=10,背景=blue,命令=self.add('two'))
p2up.grid(行=3,列=0,padx=10,pady=5)
p2down=按钮(框,text=“Player2-1”,高度=2,宽度=10,背景=blue,命令=self.subtract('two'))
p2down.grid(行=4,列=0,padx=10,pady=(5100))
重置=按钮(框架,text=“重置”,高度=2,宽度=10,命令=self.new_game())
网格(行=6,列=0,padx=10,pady=(5,20))
def新游戏(自我):
全球得分1,得分2#这些是整数
分数1=0
分数2=0
self.update_score()
def添加(自我,玩家):
全球得分1,得分2#这些是整数
self.player=玩家
如果self.player==“一个”:
#如果分数1<11:
分数1+=1
elif self.player==“两个”:
#如果得分2<11:
分数2+=1
self.update_score()
返回
def减法(自我,玩家):
全球得分1,得分2#这些是整数
self.player=玩家
如果self.player==“一个”:
如果分数1>0:
分数1=分数1-1
elif self.player==“两个”:
如果得分2>0:
得分2-=1
self.update_score()
返回
def更新_分数(自我):
全球得分1,得分2#这些是整数
score1str=str(score1)
score2str=str(score2)
self.canvas.itemconfig(self.score1_id,text=score1str)
self.canvas.itemconfig(self.score2_id,text=score2str)
返回
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
root=Tk()
gui=gui(根)
root.mainloop()

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

  reset = Button(frame,text="Reset", height=2, width=10, command=self.new_game())
None
分配给命令,因为这是
self.new\u game()
返回的结果。您应该将此更改为:

  reset = Button(frame,text="Reset", height=2, width=10, command=self.new_game)
它将函数
self.new\u game
分配给按钮的命令,而不是
None

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

 p1up = Button(frame,text="Player1 +1", height=2, width=10, background='red', command=lambda: self.add('one'))
这将为按钮的命令分配一个匿名函数,该函数调用self.add('one')


另请参见:

什么是“不走运”?你有错误吗?程序崩溃了吗?是否出现错误的单词?删除画布项,即文本,然后使用新文本执行create_text语句。请看一下删除示例,因为分数没有更新,所以没有运气。程序加载后,界面看起来很好,尽管还很简陋。我想做的是按下一个按钮,分数就会更新。我试图从画布上删除字段并重新创建它们,但所发生的一切都是显示零,并且无论我按下哪个按钮,分数在第一次加载后都不会改变。当然。wxPython也是这样。我刚刚在一天的晚些时候编写了一些Javascript页面,完全错过了这一点。工作得很有魅力!谢谢