我目前正在开发一个简单的井字游戏,以了解Python的GUI功能(我正在使用Tkinter)。我想将标签放在按钮上方,有点像标题。但是,当我运行代码时,由于某种原因,按钮会出现在标签上方。为了解决这个问题,我尝试将标签的行更改为按钮上方( mL行= 0,sL行= 1 和按钮行= 2 ),但是当我这样做时,按钮就消失了。
from tkinter import *
# Create the GUI (set title)
tk = Tk()
tk.title("Tic Tac Toe") # Create game announcer
gA = "X Goes First"
# Create Button List
buttons = []
# ---------------------------------------------------------------------------------------
# Create Board Class
class Board(Frame):
'''Creates Game Board'''
# -----------------------------------------------------------------------------------
# Constructor
def __init__(self, master):
super(Board, self).__init__(master)
self.grid()
self.create_widgets()
# -----------------------------------------------------------------------------------
# Creates Widgets (Buttons,labels,ext)
def create_widgets(self):
# main label
mL = Label(text="Tic Tac Toe", font=("Comic Sans MS", "44"), padx=50, pady=50)
mL.grid()
# game announcer
sL = Label(text=gA, font=("Comic Sans MS", "20"))
sL.grid()
# buttons
r = 2 # row variable
c = 0 # column variable
self.c_button(0, r, c)
# -----------------------------------------------------------------------------------
# Creates the button and appends to buttons list
def c_button(self, index, r, c):
buttons.append(Button(self, font=("Comic Sans MS", "22"), width=5, height=1, command=lambda: self.button_press(index)))
buttons[index].grid(row=r)
# ------------------------------------------------------------------------------------
# Acts when Button is pressed (for testing, changes color to red and adds text)
def button_press(self,index):
buttons[index].configure(bg="red",text="press")
# ---------------------------------------------------------------------------------------
# main
board = Board(tk)
tk.mainloop()
我试图使代码尽可能地可追溯。
答案 0 :(得分:0)
您的主要问题是,由于调用Label
和Button
的方式,有时将元素添加到要创建的Frame
中,有时将元素添加到顶层窗口:
mL = Label(text="Tic Tac Toe", ...)
sL = Label(text=gA, ...)
Button(self, font=("Comic Sans MS", "22"), ...)
这些都应该以相同的开头:
mL = Label(self, text="Tic Tac Toe", ...)
sL = Label(self, text=gA, ...)
Button(self, font=("Comic Sans MS", "22"), ...)
以下是我对代码所做的修改,以及一些其他修改,以消除接口全局变量并使它们成为对象的一部分:
from tkinter import *
TITLE_FONT = ("Comic Sans MS", "44")
BUTTON_FONT = ("Comic Sans MS", "22")
ANNOUNCER_FONT = ("Comic Sans MS", "20")
# Create Board Class
class Board(Frame):
''' Create Tic-Tac-Toe Game Board '''
# Constructor
def __init__(self, master):
super().__init__(master)
self.buttons = [None] * 9
self.sL = None
self.create_widgets()
self.pack()
# Create Widgets (buttons, labels, etc.)
def create_widgets(self):
# main label
Label(self, text="Tic Tac Toe", font=TITLE_FONT, padx=50, pady=50).grid(row=0, columnspan=3)
# game announcer
self.sL = Label(self, text="X Goes First", font=ANNOUNCER_FONT)
self.sL.grid(row=1, columnspan=3)
# buttons
for r in range(3): # row variable
for c in range(3): # column variable
self.c_button(r * 3 + c, r + 2, c)
# Create the button and appends to buttons list
def c_button(self, index, r, c):
self.buttons[index] = Button(self, font=BUTTON_FONT, width=5, height=1, command=lambda: self.button_press(index))
self.buttons[index].grid(row=r, column=c)
# Act when Button is pressed (for testing, changes color to red and adds text)
def button_press(self, index):
self.buttons[index].configure(bg="red", text="press")
self.sL['text'] = index
# Create the GUI
tk = Tk()
tk.title("Tic Tac Toe")
board = Board(tk)
tk.mainloop()