日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python控件随窗口变化而适配_Tkinter窗口/控件比例调整

發(fā)布時間:2023/12/1 python 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python控件随窗口变化而适配_Tkinter窗口/控件比例调整 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

我目前正在為一個編程類開發(fā)一個pythongui版本的Reversi。我已經(jīng)對游戲邏輯進行了編程,目前我正在嘗試使用Tkinter實現(xiàn)GUI。我有一些問題,調(diào)整游戲板(根窗口)和它的一切(畫布和形狀)成比例。這款游戲目前還不錯,但我試圖讓棋盤正確調(diào)整大小的一切都沒有奏效。相關代碼如下。在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ù)正在繪制的畫布的大小在板上的正確空間中繪制適當顏色的圓。在

我對調(diào)整大小問題的解決方案是將on_resize()綁定到init方法中的''事件,但這會導致程序在遞歸周期中崩潰。一般來說,我是新手。為什么on_resize()方法綁定到''導致程序崩潰?在

很抱歉代碼混亂,我仍在努力。在

謝謝。在

總結

以上是生活随笔為你收集整理的python控件随窗口变化而适配_Tkinter窗口/控件比例调整的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。