我是 Tkinter 的新手,所以請耐心等待。我正在制作一個簡單的游戲,其中涉及以網格布局的按鈕。當按下其中一個按鈕時,根據它們的位置,它們會移動到網格上的不同位置。我覺得這個錯誤可能是一個非常小的錯誤,但它繼續逃避我。
與問題相關的所有代碼:
import numpy as np
from tkinter import *
class MoveableButton(Button):
def __init__(self, location, *args, **kwargs):
super().__init__(*args, **kwargs)
self.location = location
# the function I hope to call
def move(self):
self.location = "new location"
self.grid(column=self.location[0], row=self.location[1])
root = Tk()
pieces = [MoveableButton("location_coordinates", root) for i in range(24)]
for i, piece in enumerate(pieces):
piece.command = piece.move #the source of the problem
[piece.grid(column=i%5, row=i//5) for i, piece in enumerate(pieces)]
root.mainloop()
uj5u.com熱心網友回復:
問題是您永遠不會為按鈕分配命令。您在 python 物件上設定command
屬性,但這與為實際小部件設定選項不同。
您需要洗掉piece.command = piece.move
和添加piece.configure(command=piece.move)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/504279.html
標籤:Python python-3.x tkinter tkinter 按钮
上一篇:PythonTypeError:changeMaster.__init__()缺少1個必需的位置引數:'parent'