如何像Windows上的应用程序一样打开.py文件?

发布于 2025-01-28 19:35:46 字数 4642 浏览 3 评论 0 原文

from tkinter import *
import random

command_list = []


def next_turn(r, c):
    global player
    global command_list
    global game_started
    command_list.append((r, c))
    if buttons[r][c]['text'] == "" and check_winner() is False:

        if player == players[0]:

            buttons[r][c]['text'] = player
            game_started = True
            if check_winner() is False:
                player = players[1]
                player_label.configure(text=(player + "'s turn"))
            elif check_winner() is True:
                player_label.configure(text=(player + " WINS!"))
            elif check_winner() == 'Tie':
                player_label.configure(text='TIE!')

        else:
            buttons[r][c]['text'] = player

            if check_winner() is False:
                player = players[0]
                player_label.configure(text=(player + "'s turn"))
            elif check_winner() is True:
                player_label.configure(text=(player + " WINS!"))
            elif check_winner() == 'Tie':
                player_label.configure(text='TIE!', bg='yellow')


def check_winner():
    # rows wise
    for r in range(3):
        if buttons[r][0]['text'] == buttons[r][1]['text'] == buttons[r][2]['text'] != "":
            buttons[r][0].configure(bg='green')
            buttons[r][1].configure(bg='green')
            buttons[r][2].configure(bg='green')
            return True

    # column wise
    for c in range(3):
        if buttons[0][c]['text'] == buttons[1][c]['text'] == buttons[2][c]['text'] != "":
            buttons[0][c].configure(bg='green')
            buttons[1][c].configure(bg='green')
            buttons[2][c].configure(bg='green')
            return True

    # diagonal wise
    if buttons[0][0]['text'] == buttons[1][1]['text'] == buttons[2][2]['text'] != "":
        buttons[0][0].configure(bg='green')
        buttons[1][1].configure(bg='green')
        buttons[2][2].configure(bg='green')
        return True

    if buttons[0][2]['text'] == buttons[1][1]['text'] == buttons[2][0]['text'] != "":
        buttons[0][2].configure(bg='green')
        buttons[1][1].configure(bg='green')
        buttons[2][0].configure(bg='green')
        return True

    if empty_space():
        return False
    return "Tie"


def empty_space():
    for r in range(3):
        for c in range(3):
            if buttons[r][c]['text'] == '':
                return True
    return False


def new_game():
    global player

    player = random.choice(players)

    player_label.configure(text=player + "'s turn")

    for r in range(3):
        for c in range(3):
            buttons[r][c].configure(text="", bg='grey')


def undo():
    global player
    if check_winner() is False and len(command_list) != 0:
        undo_row, undo_column = command_list[-1][0], command_list[-1][1]
        buttons[undo_row][undo_column]['text'] = ''
        if player == players[1]:
            player = players[0]
            player_label.configure(text=(player + "'s turn"))
        else:
            player = players[1]
            player_label.configure(text=(player + "'s turn"))
        command_list.pop()


mainWindow = Tk()
mainWindow.title("Tic-Tac-Toe")
mainWindow.configure(background='grey')
players = ["X", "O"]

# pick a random player.
player = random.choice(players)

# end button
end_button = Button(mainWindow, text="Close", background='red', font=('consolas', 20), command=mainWindow.destroy)
end_button.pack(anchor='w')

player_label = Label(mainWindow, text=player + "'s turn", font=('consolas', 40))
player_label.pack(side='top')

reset_button = Button(mainWindow, text='New Game', font=('consolas', 20), command=new_game)
reset_button.pack(side='top')


# undo button
undo_button = Button(mainWindow, text='Undo', font=('consolas', 20), command=undo)
undo_button.pack(side='top', anchor='e')

# Buttons structure
buttons = [[0, 0, 0],
           [0, 0, 0],
           [0, 0, 0]]

# Game Board.
board = Frame(mainWindow)
board.configure(background='grey')
board.pack()

for row in range(3):
    for column in range(3):
        buttons[row][column] = Button(board, text="", font=('consolas', 40), width=5, height=2,
                                      command=lambda bt_row=row, bt_column=column:
                                      next_turn(bt_row, bt_column))
        buttons[row][column].grid(row=row, column=column)
        buttons[row][column].configure(background='grey')


mainWindow.mainloop()

以上程序是一种名为Tic-Tak-toe的游戏。 我可以在IDE上运行该程序而不会出错。

这是我要在Windows或Linux或任何其他平台上运行的程序,而无需使用IDE或命令行,就像Word,Execel,Halo 3(Game)等其他应用程序一样,我该怎么做?

from tkinter import *
import random

command_list = []


def next_turn(r, c):
    global player
    global command_list
    global game_started
    command_list.append((r, c))
    if buttons[r][c]['text'] == "" and check_winner() is False:

        if player == players[0]:

            buttons[r][c]['text'] = player
            game_started = True
            if check_winner() is False:
                player = players[1]
                player_label.configure(text=(player + "'s turn"))
            elif check_winner() is True:
                player_label.configure(text=(player + " WINS!"))
            elif check_winner() == 'Tie':
                player_label.configure(text='TIE!')

        else:
            buttons[r][c]['text'] = player

            if check_winner() is False:
                player = players[0]
                player_label.configure(text=(player + "'s turn"))
            elif check_winner() is True:
                player_label.configure(text=(player + " WINS!"))
            elif check_winner() == 'Tie':
                player_label.configure(text='TIE!', bg='yellow')


def check_winner():
    # rows wise
    for r in range(3):
        if buttons[r][0]['text'] == buttons[r][1]['text'] == buttons[r][2]['text'] != "":
            buttons[r][0].configure(bg='green')
            buttons[r][1].configure(bg='green')
            buttons[r][2].configure(bg='green')
            return True

    # column wise
    for c in range(3):
        if buttons[0][c]['text'] == buttons[1][c]['text'] == buttons[2][c]['text'] != "":
            buttons[0][c].configure(bg='green')
            buttons[1][c].configure(bg='green')
            buttons[2][c].configure(bg='green')
            return True

    # diagonal wise
    if buttons[0][0]['text'] == buttons[1][1]['text'] == buttons[2][2]['text'] != "":
        buttons[0][0].configure(bg='green')
        buttons[1][1].configure(bg='green')
        buttons[2][2].configure(bg='green')
        return True

    if buttons[0][2]['text'] == buttons[1][1]['text'] == buttons[2][0]['text'] != "":
        buttons[0][2].configure(bg='green')
        buttons[1][1].configure(bg='green')
        buttons[2][0].configure(bg='green')
        return True

    if empty_space():
        return False
    return "Tie"


def empty_space():
    for r in range(3):
        for c in range(3):
            if buttons[r][c]['text'] == '':
                return True
    return False


def new_game():
    global player

    player = random.choice(players)

    player_label.configure(text=player + "'s turn")

    for r in range(3):
        for c in range(3):
            buttons[r][c].configure(text="", bg='grey')


def undo():
    global player
    if check_winner() is False and len(command_list) != 0:
        undo_row, undo_column = command_list[-1][0], command_list[-1][1]
        buttons[undo_row][undo_column]['text'] = ''
        if player == players[1]:
            player = players[0]
            player_label.configure(text=(player + "'s turn"))
        else:
            player = players[1]
            player_label.configure(text=(player + "'s turn"))
        command_list.pop()


mainWindow = Tk()
mainWindow.title("Tic-Tac-Toe")
mainWindow.configure(background='grey')
players = ["X", "O"]

# pick a random player.
player = random.choice(players)

# end button
end_button = Button(mainWindow, text="Close", background='red', font=('consolas', 20), command=mainWindow.destroy)
end_button.pack(anchor='w')

player_label = Label(mainWindow, text=player + "'s turn", font=('consolas', 40))
player_label.pack(side='top')

reset_button = Button(mainWindow, text='New Game', font=('consolas', 20), command=new_game)
reset_button.pack(side='top')


# undo button
undo_button = Button(mainWindow, text='Undo', font=('consolas', 20), command=undo)
undo_button.pack(side='top', anchor='e')

# Buttons structure
buttons = [[0, 0, 0],
           [0, 0, 0],
           [0, 0, 0]]

# Game Board.
board = Frame(mainWindow)
board.configure(background='grey')
board.pack()

for row in range(3):
    for column in range(3):
        buttons[row][column] = Button(board, text="", font=('consolas', 40), width=5, height=2,
                                      command=lambda bt_row=row, bt_column=column:
                                      next_turn(bt_row, bt_column))
        buttons[row][column].grid(row=row, column=column)
        buttons[row][column].configure(background='grey')


mainWindow.mainloop()

The above program is a game called Tic-Tak-Toe.
I am able to run the program on IDE without any error.

This is the program that i want to run on windows or linux or any other platform without using the IDE or command line, just like other application such as word, execel , halo 3(game) etc. How can I do it ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感情旳空白 2025-02-04 19:35:46

您需要定位操作系统并创建可执行文件。例如,您可以创建调用.py pgoram的交流程序。以下是如何为Windows做到这一点的示例。

对于Windows - 我使用Windows Ubuntu Linux子系统创建可执行的定位Windows 64位:

  • sudo apt-apt-get install install install install install install mingw-w64 阅读更多
  • 使新文件game.c
   #include <stdlib.h>

   int main() {
       system("py ./temp.py");
       return 0;
    }
  • 将您的game.py and game.c放在同一文件夹中并运行
    X86_64-W64-MINGW32-GCC -O GAME.EXE GAME.C

You need to target an operating system and create an executable file. For example you could create a c program that calls your .py pgoram. Below is an example of how to do that for windows.

For windows - I used windows Ubuntu linux subsystem to create the executable targeting windows 64-bit:

  • sudo apt-get install mingw-w64. Read more.
  • make new file game.c
   #include <stdlib.h>

   int main() {
       system("py ./temp.py");
       return 0;
    }
  • Put your game.py and game.c in the same folder and run
    x86_64-w64-mingw32-gcc -o game.exe game.c

Working Picture

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文