gpt4 book ai didi

python - 在未先打开 ide 的情况下无法打开 .py 文件

转载 作者:太空宇宙 更新时间:2023-11-03 20:19:33 24 4
gpt4 key购买 nike

我编写了一个 GUI 石头剪刀布,并使用 tkinter.ttk 在按钮上使用了图像。我可以在空闲状态下运行程序时打开该程序,但是当我双击时,它只会打开 cmd 一秒钟,并且不会执行任何操作

我已经尝试过 from tkinter.ttk import *from tkinter import ttk。我还将 python 安装到 PATH

from tkinter import *
from tkinter.ttk import *
import random


# rock button command
def clickrock():
Computer.config(text=f"Computer chose: {comp}")
if comp == "rock":
Answer.configure(text="You: DRAW")
elif comp == "paper":
Answer.configure(text="You: LOSE")
else:
Answer.configure(text="You: WIN")
rockButton.configure(state='disabled')
paperButton.configure(state='disabled')
scissorsButton.configure(state='disabled')
restart.place(x=160, y=150)

# paper button command
def clickpaper():
Computer.config(text=f"Computer chose: {comp}")
if comp == "rock":
Answer.configure(text="You: WIN")
elif comp == "paper":
Answer.configure(text="You: DRAW")
else:
Answer.configure(text="You: LOSE")
rockButton.configure(state='disabled')
paperButton.configure(state='disabled')
scissorsButton.configure(state='disabled')
restart.place(x=160, y=150)
# scissors button command
def click3():
Computer.config(text=f"Computer chose: {comp}")
if comp == "rock":
Answer.configure(text="You: LOSE")
elif comp == "paper":
Answer.configure(text="You: WIN")
else:
Answer.configure(text="You: DRAW")
rockButton.configure(state='disabled')
paperButton.configure(state='disabled')
scissorsButton.configure(state='disabled')
restart.place(x=160, y=150)

def click_restart():
restart.place_forget()
comp1 = random.randint(1,3)
Answer.config(text="You: ")
Computer.config(text="Computer chose: ")
rockButton.configure(state='normal')
paperButton.configure(state='normal')
scissorsButton.configure(state='normal')

#creates window
window = Tk()
window.title("rock paper scissors")
window.geometry("400x300")



# labels to give instructions
Label(window, text="rock paper scissors game").pack()
Label(window, text="pick a button").pack()

Computer = Label(window, text="Computer chose: ")
Computer.place(x=160, y=180)

#images
rock_photo = PhotoImage(file = "rock.png")
paper_photo = PhotoImage(file = "paper.png")
scissors_photo = PhotoImage(file = "scissors.png")

#buttons
rockButton = Button(window, image =rock_photo, command=clickrock)

paperButton = Button(window, image=paper_photo, command=clickpaper)

scissorsButton = Button(window, image = scissors_photo, command=click3)

restart = Button(window, text="RESTART", command=click_restart)

# pack buttons
rockButton.place(x=80, y=50)
paperButton.place(x=160, y=50)
scissorsButton.place(x=240, y=50)

# picking computer choice
comp1 = random.randint(1,3)

# naming comp choice
if comp1 == 1:
comp = "rock"
elif comp1 == 2:
comp = "paper"
elif comp1 == 3:
comp = "scissors"

# labels
Answer = Label(window, text="You: ")
Answer.place(x=160, y=200)





我应该能够在没有空闲的情况下打开程序,其他代码可以在没有空闲的情况下打开,但是这个不能

最佳答案

刚刚注意到您的代码缺少一行使 GUI 显示并运行的行。为了能够通过 cmd 运行应用程序/调用脚本,请添加

window.mainloop()

在最后。这应该可以帮助你开始。更多信息参见例如here .

据我了解,IDLE 是使用 tkinter 构建的,因此它已经运行 mainloop()。因此,当您从 IDLE 运行脚本时,您不需要在脚本中调用它。但是您应该调用它;-) - 更多信息,例如here .

关于python - 在未先打开 ide 的情况下无法打开 .py 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58248615/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com