簡體   English   中英

Python Tkinter按鈕小部件不會移動

[英]Python Tkinter Button Widget Won't Move

我一直在做井字游戲項目,並且我計划創建9個按鈕小部件,每個小部件上都帶有圖片。 我想移動它們,以便它們在一行中是3,在一行中是3。 這是我的代碼:

#imports:
from Tkinter import *
from PIL import ImageTk, Image

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#constants
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#classes:
class App(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
myapp = App()

class GUI:
    def __init__(self):
        self.myapp = myapp
        self.myapp.master.title("tkname")
        self.myapp.master.maxsize(2000, 1200)

    def create_and_pack_canvas(self, game_board_height, game_board_width):
        canvas = Canvas(height = game_board_height, width = game_board_width)
        canvas.pack(expand=1, fill=BOTH)
        return canvas

    def form_game_board(self):
        x = 404
        y = 150
        canvas = self.create_and_pack_canvas(1000, 1000)

        for i in range(3):
            for j in range(3):
                btn = gameButton("")
                btn.create_and_pack(canvas, x, y)
                x += 200
            x = 404
            y += 208

        self.myapp.mainloop()
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class gameButton:
    def __init__(self, picture_param):
        self.picture = ImageTk.PhotoImage(Image.open("somepic"))
        self.picture_height = self.picture.height()
        self.picture_width = self.picture.width()

    def callback(self):
        print 10

    def create_and_pack(self, canvas, x_pixels, y_pixels):
        self.b = Button(myapp, text="Print 10~", command = self.callback, image = self.picture)
        self.b.pack()
        #self.b.place(bordermode = OUTSIDE, x = x_pixels, y = y_pixels)

    def __str__(self):
        pass
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#main
def main():
    gui = GUI()
    gui.form_game_board()

if __name__ == "__main__":
    main()

請注意,有這行:

#self.b.place(bordermode = OUTSIDE, x = x_pixels, y = y_pixels)

在代碼中,我已將其作為注釋,基本上是應該移動按鈕的行。 如果您運行代碼,它將簡單地將所有按鈕連續9個放在一個按鈕的下面。 在self.b.place()行之后,它們都消失了。 也許他們移得太遠了,也許沒有,但是我認為place()無法正常工作,而且我很難弄清楚為什么。

提前致謝。

最好的選擇是使用grid幾何管理器,請參見: http : //www.effbot.org/tkinterbook/grid.htm

與其創建GameButton class ,不如在GUI class放置以下內容:

    button_list = []
    list_index_counter = 0
    row_counter = 0
    column_counter = 0
    for num in range(9):
        self.button = tk.Button(canvas,
                                text="Print 10~",
                                command=self.callback,
                                image=self.callback)
        button_list.append(self.button)
        button_list[list_index_counter].grid(row=row_counter, column=column_counter)
        list_index_counter += 1
        if column_counter < (2): # it will hit this twice
            column_counter += 1
        else:                    # then this once, then back to the other one
            row_counter += 1
            column_counter = 0

這會將您的按鈕整齊地grid化為3x3網格。

button_list允許您保留對按鈕對象的引用,使它們以后更易於訪問,您可以使用button_list[some_index].grid_info()["row"/"column"]來獲取按鈕的行或列例如被網格化。

這是創建一組相似小部件的標准方法,並且避免了使用大量相同的重復代碼生成和維護名稱的問題,而無需為其創建新類。

來自effbot的警告語:

警告:切勿在同一主窗口中混合網格和打包。 Tkinter會很樂意度過余生,以期尋求雙方經理都滿意的解決方案。 不用等待,而殺死應用程序,然后再看一遍您的代碼。 一個常見的錯誤是對某些小部件使用錯誤的父級。

最后,就像@Terry Jan Reedy所說的那樣,您不需要Canvas ,可能是Frame ,或者簡單的Tk()會更好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM