我在使用tkinter创建的python PONG游戏中无法对齐我的按钮和标签。我已经搜索了如何做到这一点的答案,但没有找到解决我问题的方法。
我试图做的是让按钮不是全部堆叠在一起。我希望左侧球员的得分与屏幕左侧对齐,右侧球员的得分与屏幕右侧对齐,并且能够理解如何将其他按钮移动到我想要的位置。
我尝试的事情:
anchor = "..."
justify = "..."
grid(row="...", column = "...")
任何帮助将不胜感激。我不知道如何解决这个问题。
我想知道它是否与我一起使用画布和按钮有关吗?
以下是我的完整程序代码:
#PONG Game
from tkinter import *
import random
#http://www.tutorialspoint.com/python/tk_anchors.htm
#Globals
ball_x1 = 480
ball_x2 = 520
ball_y1 = 280
ball_y2 = 320
left_paddle_x1 = 0
left_paddle_x2 = 20
left_paddle_y1 = 240
left_paddle_y2 = 360
right_paddle_x1 = 980
right_paddle_x2 = 1000
right_paddle_y1 = 240
right_paddle_y2 = 360
left_player_score = 0
right_player_score = 0
paused = False
ball_x_motion = 10#random.randrange(-20, 20) #actually need to make a set number but random direction
ball_y_motion = 10#random.randrange(-20, 20)
def start():
global paused
if(field.coords(ball)[0] == 480):
paused = False
ball_movement()
else:
new_game(False)
def pause():
global paused
if(paused):
paused = False
else:
paused = True
ball_movement()
def new_game(reset):
global ball_x1, ball_x2, ball_y1, ball_y2, left_paddle_x1, left_paddle_x2, left_paddle_y1, left_paddle_yw
global right_paddle_x1, right_paddle_x2, right_paddle_y1, right_paddle_y2
global paused, left_player_score, right_player_score
print("here1")
if(reset):
left_player_score = 0
right_player_score = 0
print("here")
left_player_score_label.config(text=left_player_score)
right_player_score_label.config(text=right_player_score)
left_paddle_x1 = 0
left_paddle_x2 = 20
left_paddle_y1 = 240
left_paddle_y2 = 360
right_paddle_x1 = 980
right_paddle_x2 = 1000
right_paddle_y1 = 240
right_paddle_y2 = 360
field.coords(left_paddle, left_paddle_x1, left_paddle_y1, left_paddle_x2, left_paddle_y2)
field.coords(right_paddle, right_paddle_x1, right_paddle_y1, right_paddle_x2, right_paddle_y2)
paused = True
print("here2")
#starting positions
ball_x1 = 480
ball_x2 = 520
ball_y1 = 280
ball_y2 = 320
field.coords(ball, ball_x1, ball_y1, ball_x2, ball_y2)
#Event Handlers
def ball_movement():
#while(variable = true) move ball
# move the object
global ball_x_motion, ball_y_motion, ball_x1, ball_x2, ball_y1, ball_y2, paused
global left_player_score, right_player_score
#keep ball on playing field horizontally if ball hits paddles
print(((((left_paddle_y1 + left_paddle_y2)/2) - ((ball_y1 + ball_y2)/2))))
if(((ball_x1 <= 20) and (-60 <= ((((left_paddle_y1 + left_paddle_y2)/2) - ((ball_y1 + ball_y2)/2))) <= 60)) or ((ball_x2 >= 980) and (-60 <= ((((right_paddle_y1 + right_paddle_y2)/2) - ((ball_y1 + ball_y2)/2))) <= 60))):
ball_x_motion = -ball_x_motion
#increase speed
elif(ball_x1 <=0):
right_player_score += 1
new_game(False)
#paused = True
right_player_score_label.config(text=right_player_score)
elif(ball_x2 >= 1000):
left_player_score += 1
new_game(False)
#paused = True
left_player_score_label.config(text=left_player_score)
if(field.coords(ball)[1] <= 0 or field.coords(ball)[3] >= 600): #keep ball in playing field vertically
ball_y_motion = -ball_y_motion
#increase speed
if(not paused):
field.coords(ball, ball_x1 + ball_x_motion, ball_y1 + ball_y_motion, ball_x2 + ball_x_motion, ball_y2 + ball_y_motion)
ball_x1 = field.coords(ball)[0]
ball_x2 = field.coords(ball)[2]
ball_y1 = field.coords(ball)[1]
ball_y2 = field.coords(ball)[3]
#ball moves every 10 milliseconds
field.after(10, ball_movement)
def move_left_paddle_up(event):
global left_paddle_y1, left_paddle_y2
#print(event.keysym)
if(field.coords(left_paddle)[1] > 0):
field.move("left_paddle", 0, -40)
left_paddle_y1 = field.coords(left_paddle)[1]
left_paddle_y2 = field.coords(left_paddle)[3]
def move_left_paddle_down(event):
global left_paddle_y1, left_paddle_y2
if(field.coords(left_paddle)[3] < 600):
field.move("left_paddle", 0, 40)
left_paddle_y1 = field.coords(left_paddle)[1]
left_paddle_y2 = field.coords(left_paddle)[3]
def move_right_paddle_up(event):
global right_paddle_y1, right_paddle_y2
if(field.coords(right_paddle)[1] > 0):
field.move("right_paddle", 0, -40)
right_paddle_y1 = field.coords(right_paddle)[1]
right_paddle_y2 = field.coords(right_paddle)[3]
#print(event.keysym)
def move_right_paddle_down(event):
global right_paddle_y1, right_paddle_y2
if(field.coords(right_paddle)[3] < 600):
field.move("right_paddle", 0, 40)
right_paddle_y1 = field.coords(right_paddle)[1]
right_paddle_y2 = field.coords(right_paddle)[3]
#print(event.keysym)
#Canvas
master = Tk() #master
field = Canvas(master, width=1000, height=600, bg="black") #playing field
master.bind('<w>', move_left_paddle_up) #left paddle up
master.bind('<s>', move_left_paddle_down) #left paddle down
master.bind('<Up>', move_right_paddle_up) #right paddle up
master.bind('<Down>', move_right_paddle_down) #right paddle down
#create field border lines with a rectangle
border = field.create_rectangle(0, 0, 1000, 600, fill="gray")
#create midline
midline = field.create_line(500, 0, 500, 600, fill="white", dash=(4, 4))
#create ball
ball = field.create_oval(ball_x1, ball_y1, ball_x2, ball_y2, fill="white", tags=("ball"))
#create left paddle
left_paddle = field.create_rectangle(left_paddle_x1, left_paddle_y1, left_paddle_x2, left_paddle_y2, fill="red", tags=("left_paddle"))
#create right paddle
right_paddle = field.create_rectangle(right_paddle_x1, right_paddle_y1, right_paddle_x2, right_paddle_y2, fill="blue", tags=("right_paddle"))
#create Pause Button
pause_button = Button(master, text="Pause/Unpause", command=pause)
#pause_button.grid(row=1, column=1)
pause_button.pack()
#create Start Button
start_button = Button(master, text="Start", command=start)
#start_button.grid(row=1, column=3)
start_button.pack()
#left player scorekeeper
left_player_score_label = Label(master, text=left_player_score)
left_player_score_label.pack()
#right player scorekeeper
right_player_score_label = Label(master, text=right_player_score)
right_player_score_label.pack()
#create New Game Button
new_game_button = Button(master, text="New Game", command=lambda: new_game(True))
#new_game_button.grid(row=1, column=2)
new_game_button.pack()
#pack it all together on the screen
field.pack()
#execute loop to run screen processing
mainloop()
修改
这是从pack()更改为grid()后的最终结果。我意识到你不能同时使用两者,而是使用其中一种。
#PONG Game
from tkinter import *
import random
#http://www.tutorialspoint.com/python/tk_anchors.htm
#Globals
ball_x1 = 480
ball_x2 = 520
ball_y1 = 280
ball_y2 = 320
left_paddle_x1 = 0
left_paddle_x2 = 20
left_paddle_y1 = 240
left_paddle_y2 = 360
right_paddle_x1 = 980
right_paddle_x2 = 1000
right_paddle_y1 = 240
right_paddle_y2 = 360
left_player_score = 0
right_player_score = 0
paused = False
direction_factor = random.randrange(0,1)
ball_x_motion = 5
ball_y_motion = random.randrange(-5, 5)
if direction_factor == 0:
ball_x_motion *= -1
def start():
global paused
if(field.coords(ball)[0] == 480):
paused = False
ball_movement()
else:
new_game(False)
def pause():
global paused
if(paused):
paused = False
else:
paused = True
ball_movement()
def new_game(reset):
global ball_x1, ball_x2, ball_y1, ball_y2, left_paddle_x1, left_paddle_x2, left_paddle_y1, left_paddle_yw
global right_paddle_x1, right_paddle_x2, right_paddle_y1, right_paddle_y2
global paused, left_player_score, right_player_score, ball_x_motion, ball_y_motion, direction_factor
direction_factor = random.randrange(0,1)
ball_x_motion = 10
ball_y_motion = random.randrange(-10, 10)
if direction_factor == 0:
ball_x_motion *= -1
if(reset):
left_player_score = 0
right_player_score = 0
left_player_score_label.config(text=left_player_score)
right_player_score_label.config(text=right_player_score)
left_paddle_x1 = 0
left_paddle_x2 = 20
left_paddle_y1 = 240
left_paddle_y2 = 360
right_paddle_x1 = 980
right_paddle_x2 = 1000
right_paddle_y1 = 240
right_paddle_y2 = 360
field.coords(left_paddle, left_paddle_x1, left_paddle_y1, left_paddle_x2, left_paddle_y2)
field.coords(right_paddle, right_paddle_x1, right_paddle_y1, right_paddle_x2, right_paddle_y2)
paused = True
#starting positions
ball_x1 = 480
ball_x2 = 520
ball_y1 = 280
ball_y2 = 320
field.coords(ball, ball_x1, ball_y1, ball_x2, ball_y2)
#Event Handlers
def ball_movement():
#while(variable = true) move ball
# move the object
global ball_x_motion, ball_y_motion, ball_x1, ball_x2, ball_y1, ball_y2, paused
global left_player_score, right_player_score
#keep ball on playing field horizontally if ball hits paddles
#print(((((left_paddle_y1 + left_paddle_y2)/2) - ((ball_y1 + ball_y2)/2))))
if(((ball_x1 <= 20) and (-60 <= ((((left_paddle_y1 + left_paddle_y2)/2) - ((ball_y1 + ball_y2)/2))) <= 60)) or ((ball_x2 >= 980) and (-60 <= ((((right_paddle_y1 + right_paddle_y2)/2) - ((ball_y1 + ball_y2)/2))) <= 60))):
ball_x_motion *= -1.1
#ball_y_motion = ball_y_motion + 5
elif(ball_x1 <=0):
right_player_score += 1
new_game(False)
#paused = True
right_player_score_label.config(text=right_player_score)
elif(ball_x2 >= 1000):
left_player_score += 1
new_game(False)
#paused = True
left_player_score_label.config(text=left_player_score)
if(field.coords(ball)[1] <= 0 or field.coords(ball)[3] >= 600): #keep ball in playing field vertically
ball_y_motion = -ball_y_motion
if(not paused):
field.coords(ball, ball_x1 + ball_x_motion, ball_y1 + ball_y_motion, ball_x2 + ball_x_motion, ball_y2 + ball_y_motion)
ball_x1 = field.coords(ball)[0]
ball_x2 = field.coords(ball)[2]
ball_y1 = field.coords(ball)[1]
ball_y2 = field.coords(ball)[3]
#ball moves every 10 milliseconds
field.after(10, ball_movement)
def move_left_paddle_up(event):
global left_paddle_y1, left_paddle_y2
#print(event.keysym)
if(field.coords(left_paddle)[1] > 0):
field.move("left_paddle", 0, -40)
left_paddle_y1 = field.coords(left_paddle)[1]
left_paddle_y2 = field.coords(left_paddle)[3]
def move_left_paddle_down(event):
global left_paddle_y1, left_paddle_y2
if(field.coords(left_paddle)[3] < 600):
field.move("left_paddle", 0, 40)
left_paddle_y1 = field.coords(left_paddle)[1]
left_paddle_y2 = field.coords(left_paddle)[3]
def move_right_paddle_up(event):
global right_paddle_y1, right_paddle_y2
if(field.coords(right_paddle)[1] > 0):
field.move("right_paddle", 0, -40)
right_paddle_y1 = field.coords(right_paddle)[1]
right_paddle_y2 = field.coords(right_paddle)[3]
#print(event.keysym)
def move_right_paddle_down(event):
global right_paddle_y1, right_paddle_y2
if(field.coords(right_paddle)[3] < 600):
field.move("right_paddle", 0, 40)
right_paddle_y1 = field.coords(right_paddle)[1]
right_paddle_y2 = field.coords(right_paddle)[3]
#print(event.keysym)
#Canvas
master = Tk() #master
field = Canvas(master, width=1000, height=600, bg="black") #playing field
field.grid(row=4, columnspan=10)
#field.pack()
master.bind('<w>', move_left_paddle_up) #left paddle up
master.bind('<s>', move_left_paddle_down) #left paddle down
master.bind('<Up>', move_right_paddle_up) #right paddle up
master.bind('<Down>', move_right_paddle_down) #right paddle down
#create field border lines with a rectangle
border = field.create_rectangle(0, 0, 1000, 600, fill="gray")
#create midline
midline = field.create_line(500, 0, 500, 600, fill="white", dash=(4, 4))
#create ball
ball = field.create_oval(ball_x1, ball_y1, ball_x2, ball_y2, fill="white", tags=("ball"))
#create left paddle
left_paddle = field.create_rectangle(left_paddle_x1, left_paddle_y1, left_paddle_x2, left_paddle_y2, fill="red", tags=("left_paddle"))
#create right paddle
right_paddle = field.create_rectangle(right_paddle_x1, right_paddle_y1, right_paddle_x2, right_paddle_y2, fill="blue", tags=("right_paddle"))
#create Pause Button
pause_button = Button(master, text="Pause/Unpause", command=pause)
pause_button.grid(row=1, column=6)
#pause_button.pack()
#create Start Button
start_button = Button(master, text="Start", command=start)
start_button.grid(row=1, column=4)
#start_button.pack()
#left player scorekeeper
left_player_score_label = Label(master, text=left_player_score)
left_player_score_label.grid(row=1, column=0)
#left_player_score_label.pack()
#right player scorekeeper
right_player_score_label = Label(master, text=right_player_score)
right_player_score_label.grid(row=1, column=9)
#right_player_score_label.pack()
#create New Game Button
new_game_button = Button(master, text="New Game", command=lambda: new_game(True))
new_game_button.grid(row=1, column=5)
#new_game_button.pack()
#pack it all together on the screen
#field.pack()
#execute loop to run screen processing
mainloop()
答案 0 :(得分:0)
我意识到你不能同时使用网格管理器和包经理,而是使用网格管理器和包管理器。所以我删除了我所拥有的pack(),并使用了所有grid()&s并发布了最终结果。