0

初めて Python で tkinter を使用して GUI を作成しました。私は wxPython で作業しましたが、これと同じ問題はありませんでしたが、キャンバスを使用したことはありません。キャンバスの背後にある思考プロセスは、最終的にアニメーションを追加することです。グリッドの 1 つがキャンバスであるグリッド レイアウトを作成しました。itemconfig を使用してスコア フィールドを更新しようとしていますが、キャンバス上で 2 つのスコア フィールドを更新することができません。私のコードは以下です。キャンバス上の 2 つのスコアを更新する方法に関する推奨事項はありますか? 新しいスコアが画面に書き込まれる前に古いスコアを削除したい。ご覧いただきありがとうございます。グローバルを許してください。これが進行するにつれて、それらは削除されます。

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()
4

1 に答える 1

2

問題は、ボタンに関数を割り当てるのではなく、関数呼び出しの値を返すことです。例えば:

  reset = Button(frame,text="Reset", height=2, width=10, command=self.new_game())

Noneそれが返されるため、コマンドに割り当てself.new_game()ます。これを次のように変更する必要があります。

  reset = Button(frame,text="Reset", height=2, width=10, command=self.new_game)

self.new_gameではなく、ボタンのコマンドに機能を割り当てますNone

次に、他のボタンについて、ボタンのコマンドに引数を渡すには、次のようなラムダ関数を使用する必要があります。

 p1up = Button(frame,text="Player1 +1", height=2, width=10, background='red', command=lambda: self.add('one'))

self.add('one')これにより、ボタンのコマンドを呼び出す無名関数が割り当てられます。

参照:宣言時にボタン パラメータ「コマンド」が実行されるのはなぜですか?

于 2014-12-18T11:02:51.860 に答える