1

I coded a gui rock paper scissors and used images on the buttons with tkinter.ttk. I can open the program when running it through idle but when I got to double click it just opens cmd for a second and doesn't do anything

I've tried both from tkinter.ttk import * and from tkinter import ttk. I also have python installed into 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)





I should be able to open the programme without idle, other code can be opened without idle but this can't

  • 2
    did you try to run it from `cmd`? I mean open `cmd`, cd to the folder where your script is, then run `python your_script.py` – FObersteiner Oct 05 '19 at 13:35
  • no, will this allow it to run? – Silas Hayes-Williams Oct 05 '19 at 15:53
  • Give it a try. That way, you hand over the .py file's content (your code) to the python interpreter. Otherwise, your computer might not know what to do with it. – FObersteiner Oct 05 '19 at 16:06
  • I get this `D:\silas\python\tkinter\rockpaper>python Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> rps.py Traceback (most recent call last): File "", line 1, in NameError: name 'rps' is not defined` – Silas Hayes-Williams Oct 06 '19 at 12:40
  • I meant [like this](https://www.wikihow.com/Use-Windows-Command-Prompt-to-Run-a-Python-File), not entering Python first ;-) – FObersteiner Oct 06 '19 at 16:37
  • I followed the steps fully, the programme still doesn't open and I get no error message – Silas Hayes-Williams Oct 07 '19 at 21:39
  • Seems I didn't notice that the code is missing a `mainloop()` so it would not run "outside" of IDLE. Added an answer with a little explanation. – FObersteiner Oct 08 '19 at 07:02

3 Answers3

0

You can just copy all your code and paste it on a notepad. Remember to select "All Files" instead of ".txt text file". Also, save your file with a '.py' extension. After you save it, you may be able to see a '.py' file on your desktop screen. Just open it with Python launcher or normal python and you will be able to run it without your ide. just make sure after displaying the output, it will disappear immediately. For that, You can use at the end:

input("")
0

Maybe you have 2 versions of python installed on your computer and by default it's the other version who launch your python file when you try to execute it.

Can you please give us some more informations ? Like the code for example

Mathix420
  • 872
  • 11
  • 21
0

just noticed that your code is missing a line that makes the GUI appear and run. To be able to run the app / call the script via cmd, add

window.mainloop()

at the end. That should get you started. More info see e.g. here.

As I understand, IDLE is built with tkinter, so it already runs mainloop(). Therefore, you don't need to call that in your script as you run it from IDLE. However you should call it ;-) - more info e.g. here.

FObersteiner
  • 22,500
  • 8
  • 42
  • 72