Я написал функцию для кнопки перезапуска, которая запускает мою программу с самого начала. Однако я столкнулся с проблемой, которая, я думаю, может быть специфической для MacOS. Когда моя программа перезапускается, она работает в доке, а не на экране. В результате пользователь должен нажать на программу, чтобы открыть ее.
Я бы предпочел, чтобы окно программы запускалось прямо на экран.
Вот функция перезапуска программы:
def restart_program():
python = sys.executable
os.execl(python, python, * sys.argv)
os.system('''/usr/bin/osascript -e 'tell app "Finder" to set frontmost of process "Python" to true' ''')
Вот моя полная программа:
from Tkinter import *
from Tkinter import Canvas
import sys
import os
import random
import time
window1 = Tk()
window1.title("Snake")
window1.config(background="black")
window1.geometry("400x400")
coord1 = 0
coord2 = 0
coord3 = 20
coord4 = 20
space_right = 20
space_left = 400
space_up = 400
space_down = 20
score = 0
def restart_program():
python = sys.executable
os.execl(python, python, * sys.argv)
os.system('''/usr/bin/osascript -e 'tell app "Finder" to set frontmost of process "Python" to true' ''')
def quit__window():
quit_window = Toplevel(window1)
quit_window.overrideredirect(1)
quit_window.geometry("230x110+90+50")
quit_window.config(background="black")
canvas2 = Canvas(quit_window, width=230, height=110, bg="black", bd=0, highlightthickness=1, relief="ridge",
highlightbackground="black")
canvas2.pack()
lbl1 = canvas2.create_text(115, 20, text="Game Over!", fill="white", font=("arial bold", 20))
lbl2 = canvas2.create_text(115, 50, text="Your Score: {0}".format(score), fill="white", font=("arial", 20))
restart = Button(canvas2, text="Play Again", highlightbackground="black", command=restart_program)
restart_window = canvas2.create_window(115, 80, window=restart)
def generate_food():
global food
random1 = random.choice(
[20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250,
260, 270, 280, 290, 300, 310, 320, 330, 340, 350, 360, 370, 380])
random2 = random.choice(
[20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250,
260, 270, 280, 290, 300, 310, 320, 330, 340, 350, 360, 370, 380])
food = canvas.create_rectangle(random1 - 10, random2 - 10, random1 + 10, random2 + 10, fill="red")
def move_ball_right(event):
global space_right
global space_left
global score
while True:
xspeed = 10
yspeed = 0
try:
canvas.move(ball, xspeed, yspeed)
Tk.update(window1)
space_right = space_right + 10
space_left = space_left + 10
ball_pos = canvas.coords(ball)
food_pos = canvas.coords(food)
if ball_pos == food_pos:
canvas.delete(food)
generate_food()
score = score + 1
Tk.update(window1)
if space_right == 400:
quit__window()
time.sleep(0.1)
except:
break
def move_ball_left(event):
global space_right
global space_left
global score
while True:
xspeed = -10
yspeed = 0
try:
canvas.move(ball, xspeed, yspeed)
Tk.update(window1)
space_left = space_left - 10
space_right = space_right - 10
ball_pos = canvas.coords(ball)
food_pos = canvas.coords(food)
if ball_pos == food_pos:
canvas.delete(food)
generate_food()
score = score + 1
Tk.update(window1)
if space_left == 400:
quit__window()
time.sleep(0.1)
except:
break
def move_ball_down (event):
global space_down
global space_up
global score
while True:
xspeed = 0
yspeed = 10
try:
canvas.move(ball, xspeed, yspeed)
Tk.update(window1)
space_down = space_down + 10
space_up = space_up + 10
ball_pos = canvas.coords(ball)
food_pos = canvas.coords(food)
if ball_pos == food_pos:
canvas.delete(food)
generate_food()
score = score + 1
Tk.update(window1)
if space_down == 400:
quit__window()
time.sleep(0.1)
except:
break
def move_ball_up (event):
global space_down
global space_up
global score
while True:
xspeed = 0
yspeed = -10
try:
canvas.move(ball, xspeed, yspeed)
Tk.update(window1)
space_up = space_up - 10
space_down = space_down - 10
ball_pos = canvas.coords(ball)
food_pos = canvas.coords(food)
if ball_pos == food_pos:
canvas.delete(food)
generate_food()
score = score + 1
Tk.update(window1)
if space_up == 400:
quit__window()
time.sleep(0.1)
except:
break
canvas = Canvas(window1, width=400, height=400, bg="black", bd=0, highlightthickness=1, relief="ridge", highlightbackground = "black")
canvas.pack()
ball = canvas.create_rectangle(coord1, coord2, coord3, coord4, fill="white")
generate_food()
window1.bind('<Right>', move_ball_right)
window1.bind('<Left>', move_ball_left)
window1.bind('<Down>', move_ball_down)
window1.bind('<Up>', move_ball_up)
window1.mainloop()
Любая помощь будет оценена!