python控件随窗口变化而适配_Tkinter窗口/控件比例调整
我目前正在為一個(gè)編程類(lèi)開(kāi)發(fā)一個(gè)pythongui版本的Reversi。我已經(jīng)對(duì)游戲邏輯進(jìn)行了編程,目前我正在嘗試使用Tkinter實(shí)現(xiàn)GUI。我有一些問(wèn)題,調(diào)整游戲板(根窗口)和它的一切(畫(huà)布和形狀)成比例。這款游戲目前還不錯(cuò),但我試圖讓棋盤(pán)正確調(diào)整大小的一切都沒(méi)有奏效。相關(guān)代碼如下。在class OthelloApplication:
def __init__(self, game_state: othello.Othello):
self._game_state = game_state
self._game_state.new_game()
self._game_board = game_state.show_board()
self._root_window = tkinter.Tk()
for row in range(self._game_state.show_rows()):
for col in range(self._game_state.show_cols()):
canvas = tkinter.Canvas(self._root_window, width = 100, height = 100,
borderwidth = 0, highlightthickness = 1,
background = _BACKGROUND_COLOR, highlightbackground = 'black')
canvas.grid(row = row, column = col, padx = 0, pady = 0,
sticky = tkinter.N + tkinter.S + tkinter.E + tkinter.W)
self._root_window.rowconfigure(row, weight = 1)
self._root_window.columnconfigure(col, weight = 1)
self._turn_window = tkinter.Canvas(self._root_window, width = 200,
height = 100, highlightthickness = 0, background = 'white')
self._root_window.bind('', self._on_canvas_clicked)
self._root_window.bind('', self.on_resize)
def draw_game_pieces(self) -> None:
for row in range(self._game_state.show_rows()):
for col in range(self._game_state.show_cols()):
if self._game_board[col][row] == ' ':
pass
else:
canvas = tkinter.Canvas(master = self._root_window, width = 100, height = 100,
borderwidth = 0, highlightthickness = 1,
background = _BACKGROUND_COLOR, highlightbackground = 'black')
canvas.grid(row = row, column = col, padx = 0, pady = 0,
sticky = tkinter.N + tkinter.S + tkinter.E + tkinter.W)
canvas.update()
canvas.create_oval(2, 2, canvas.winfo_width() - 2,
canvas.winfo_height() - 2, fill = self._which_color(col,
row), outline = self._which_color(col, row))
self._root_window.rowconfigure(row, weight = 1)
self._root_window.columnconfigure(col, weight = 1)
def display_turn(self) -> None:
if self._game_state.show_turn() == 'B':
turn = 'black'
else:
turn = 'white'
self._turn_window.grid(row = self._game_state.show_rows() // 2 - 1,
column = self._game_state.show_cols() + 1,
sticky = tkinter.N + tkinter.S + tkinter.E + tkinter.W)
self._turn_info = self._turn_window.create_text(10, 10,
font = 'Helvetica', anchor = 'nw')
self._turn_window.itemconfig(self._turn_info, text = 'Turn = ' + turn)
def on_resize(self, event: tkinter.Event) -> None:
self.draw_game_pieces()
def _which_color(self, col: int, row: int) -> str:
if self._game_board[col][row] == 'B':
return 'black'
elif self._game_board[col][row] == 'W':
return 'white'
def _on_canvas_clicked(self, event: tkinter.Event) -> (int):
print(event.widget.winfo_reqwidth(), event.widget.winfo_reqheight())
print(event.widget.winfo_width(), event.widget.winfo_height())
try:
grid_info = event.widget.grid_info()
move = (int(grid_info["row"]), int(grid_info["column"]))
self._game_state.player_move(move[1], move[0])
self.draw_game_pieces()
self.display_turn()
except AttributeError:
pass
except othello.InvalidMoveError:
print('Error: that wasn\'t a valid move.')
except othello.GameOverError:
print('The game is over.')
def start(self) -> None:
self.draw_game_pieces()
self.display_turn()
self._root_window.mainloop()
draw_game_pieces()方法根據(jù)正在繪制的畫(huà)布的大小在板上的正確空間中繪制適當(dāng)顏色的圓。在
我對(duì)調(diào)整大小問(wèn)題的解決方案是將on_resize()綁定到init方法中的''事件,但這會(huì)導(dǎo)致程序在遞歸周期中崩潰。一般來(lái)說(shuō),我是新手。為什么on_resize()方法綁定到''導(dǎo)致程序崩潰?在
很抱歉代碼混亂,我仍在努力。在
謝謝。在
總結(jié)
以上是生活随笔為你收集整理的python控件随窗口变化而适配_Tkinter窗口/控件比例调整的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 颐和园残疾人需要预约吗
- 下一篇: 头部ct能检查出什么_【安全用药】做CT