无法先打开IDE才能打开.py文件

时间:2019-10-05 13:10:27

标签: python python-3.x tkinter

我给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)





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

3 个答案:

答案 0 :(得分:0)

您可以复制所有代码并将其粘贴到记事本上。记住选择“所有文件”而不是“ .txt文本文件”。另外,以“ .py”扩展名保存文件。保存后,您可能可以在桌面屏幕上看到“ .py”文件。只需使用Python启动器或普通python打开它,您就可以在不需要您的ide的情况下运行它。只要确保在显示输出后,它就会立即消失。为此,您可以在最后使用:

input("")

答案 1 :(得分:0)

也许您的计算机上安装了2个版本的python,默认情况下,当您尝试执行python文件时,另一个版本会启动python文件。

您能给我们更多的信息吗?像这样的代码

答案 2 :(得分:0)

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

window.mainloop()

最后。那应该让您开始。更多信息请参见here

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