import tkinter as tk
from tkinter import *
HEIGHT = 600
WIDTH = 600
root = tk.Tk()
def button_event1():
import ThreePlayers
print(ThreePlayers)
def button_event2():
import TwoPlayersGame
print(TwoPlayersGame)
def button_event3():
print("")
def button_event4():
print("")
def button_event5():
quit()
root = Tk()
root.title('Connect 4 Game')
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()
L = Label(root, text="Welcome to 3P Connect 4!!!",font=("Ariel",20,"bold", "underline"))
L.config(anchor=CENTER)
L.pack()
button = tk.Button(root, text="3 Player Game", command=button_event1)
button.pack()
button = tk.Button(root, text="2 Player Game", command=button_event2)
button.pack()
button = tk.Button(root, text="1 Player Game", command=button_event3)
button.pack()
button = tk.Button(root, text="Options", command=button_event4)
button.pack()
button = tk.Button(root, text="QUIT", command=button_event5)
button.pack()
root.mainloop()
以上是我的 Tkinter GUI 代碼,但我希望在根/視窗的中心有標簽,我該怎么做?目前它坐在按鈕的頂部,其他一切都很好,按鈕事件和這樣的作品
uj5u.com熱心網友回復:
在我看來,您應該使用 place 或 grid 而不是 pack。因為 pack 只提供很少的對齊選項。
否則,可以將主視窗分成兩個框架,然后將標簽打包在下框架的頂部
frame = Frame(root)
frame.pack()
bottomframe = Frame(root)
bottomframe.pack( side = BOTTOM )
L = Label(root, text="Welcome to 3P Connect 4!!!",font=("Ariel",20,"bold", "underline"))
L.pack(side = TOP)
我希望這會有所幫助。但您應該使用網格來更好地對齊或放置。
uj5u.com熱心網友回復:
您可以使用以下命令將標簽放置在中心
L = Label(root, text="Welcome to 3P Connect 4!!!",font=("Ariel",20,"bold", "underline"))
# L.config(anchor=CENTER)
# L.pack()
L.place(x=HEIGHT/2, y=WIDTH/2, anchor="center")
同樣,您也可以使用button.place(x=100, y=25)for 按鈕
REF:Tkinter:固定尺寸框架中的中心標簽?
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/394634.html
下一篇:在串列中拆分這些字串
