【发布时间】:2018-03-21 21:06:53
【问题描述】:
所以我有一个棋盘 GUI,它显示一个带有棋子的棋盘和一个允许用户输入棋子移动的 UI。目前我的 GUI 和 UI 都在工作,但是当我运行程序时,UI 在没有 GUI 的情况下运行。有什么方法可以同时运行 GUI 和 UI?
我的主要课程:
class ChessBoard(tk.Frame):
def __init__(self, parent, rows=8, columns=8, size=70, color1="white", color2="lightgrey"):
self.rows = rows
self.columns = columns
self.size = size
self.color1 = color1
self.color2 = color2
self.pieces = {}
canvas_width = columns * size
canvas_height = rows * size
tk.Frame.__init__(self, parent)
self.canvas = tk.Canvas(self, borderwidth=0, highlightthickness=0,width=canvas_width, height=canvas_height, background="white")
self.canvas.pack(side="top", fill="both", expand=True, padx=2, pady=2)
color = self.color2
for row in range(self.rows):
color = self.color1 if color == self.color2 else self.color2
for col in range(self.columns):
x1 = (col * self.size)
y1 = (row * self.size)
x2 = x1 + self.size
y2 = y1 + self.size
self.canvas.create_rectangle(x1, y1, x2, y2, outline="black", fill=color, tags="square")
color = self.color1 if color == self.color2 else self.color2
我的用户界面:
def UserInput(self):
KingRow = int(input("Choose Row: "))
KingColumn = int(input("Choose Column: "))
#Not Complete UI
这是调用所有内容的地方:
if __name__ == "__main__":
root = tk.Tk()
board = ChessBoard(root)
board.pack(side="top", fill="both", expand="true", padx=4, pady=4)
board.UserInput()
root.mainloop()
到目前为止,我尝试的是 Root.after() 但似乎什么也没发生(我可能用错了)
GUI 运行的唯一时间是 UI 中出现错误时。有什么方法可以同时运行 GUI 和 UI?谢谢!
【问题讨论】:
-
你永远不会创建画布,所以你的程序应该会抛出错误。
-
请提供Minimal, Complete, and Verifiable example。这将使我们能够帮助您解决问题。您的代码不足以重现问题。如果没有 MCVE,您将不可能得到您正在寻找的答案。
-
为什么不用弹出对话框输入行/列信息,而不必从命令行输入?
-
@Mike-SMT 正在创建画布,我的代码中当前没有错误。
-
不是来自您提供的代码。从我所见,您从未创建过画布,并且确实遇到了错误。
AttributeError: 'ChessBoard' object has no attribute 'canvas'。如果您没有在示例中提供代码,您不能指望人们认为您已经创建了画布。无法解决不存在的问题。
标签: python-3.x python-2.7 tkinter tkinter-canvas