I had that problem a few days ago about running external python programs, which I know know can be done with

execfile("filename.py")

But I now have a new problem....

Here is my program I am making for myself:

import random
from Tkinter import *
mainWindow = Tk()

strGame=""

def dieSimulator():
    number = random.randint(1,6)
    if number == 1:
        #print "UNO"
        strGame = "UNO"
        answerLabel.configure(text=strGame)
    elif number == 2:
        #print "SMASH TV"
        strGame = "SMASH TV"
        answerLabel.configure(text=strGame)
    elif number == 3:
        #print "HALO"
        strGame = "HALO"
        answerLabel.configure(text=strGame)
    elif number == 4:
        #print "PDZ"
        strGame = "PDZ"
        answerLabel.configure(text=strGame)
    elif number == 5:
        #print "COD"
        strGame = "COD"
        answerLabel.configure(text=strGame)
    elif number == 6:
        #print "GRAW"
        strGame = "GRAW"
        answerLabel.configure(text=strGame)

    number2 = random.randint(1,2)
    restart = dieSimulator
    if number2 == 1:
        strGame2 = "Approved"
        answerLabel2.configure(text=strGame2)
    elif number2 == 2:
        strGame2 = "Denied"
        answerLabel2.configure(text=strGame2)
                

startButton = Button(mainWindow, text="Start", command=dieSimulator)
startButton.grid(row=0, column=0)

answerLabel = Label(mainWindow, text="Game...")
answerLabel.grid(row=0, column=1)

answerLabel2 = Label(mainWindow, text="Approved/Denied...")
answerLabel2.grid(row=1, column=1)

mainloop()

When I run it, it does exactly what it's supposed to do, however I would like it to make a new choice if

strGame2 = "Denied"

And I would like for there to be a half second delay when it shows the game, as if it were taking to decide the game, and then taking another half second to decide if it's approved or not.

Another thing I would like, but can't figure out either, is I want to start button's text to change to "Restart" from "Start" once it's been pressed once.

Thanks for your time and help guys...

Recommended Answers

All 9 Replies

I want it to be exactly like this

#Dedicated to Defender117's "Die of Decision" and "coin of choice"....
# [version 1.8]

print " "
import random 
import time
strGame = 0

number = random.randint(1,6)
if number == 1:
    print "UNO"
    strGame = "UNO"
elif number == 2:
    print "SMASH TV"
    strGame = "SMASH TV"
elif number == 3:
    print "HALO"
    strGame = "HALO"
elif number == 4:
    print "PDZ"
    strGame = "PDZ"
elif number == 5:
    print "COD"
    strGame = "COD"
elif number == 6:
    print "GRAW"
    strGame = "GRAW"

number2 = random.randint(1,2)
if number2 == 1:
    time.sleep(0.5)
    print "approved\n"
    vetoIt = raw_input("Veto It?" )
    if vetoIt == "y":
        execfile("die of decision simulator.py")
    if vetoIt == "n":
        print "Then the decision stands:",strGame
    

elif number2 == 2:
    time.sleep(0.5)
    print "denied"
    time.sleep(0.5)
    execfile("die of decision simulator.py")

But to run entirely in a window with a start button...

Well, I can help with the startButton text:

def dieSimulator():
    startButton.configure(text="Restart")
   # rest of your code .......

Thank you, my program is imporving. But I still need it to restart - choose a new game to be displayed, if it displays "denied".

the half a second delay can be done either using:

import time
time.sleep(0.5)

that would be the simplest however the whole code including the GUI would hang as if it crashed, so a slightly more complicated method is doing a while loop, checking the time on each pass, and if it is more than half a second, exiting the loop, and continuing, which is done with the following code:

from time import time

def Delay(delay):
    stopTime = time()+delay
        while time() < stopTime:
            pass
        return

Just whack the above function into your code then whenever you want a time delay, just put Delay(0.5) into your code, and it should work

happy coding,
a1eio

not sure what you mean with the other request, but i think i can help you, i've done loads of work with simple gui's and stuff, just say what you need doing exactly, i'm a bit simple when it comes to understanding stuff

the program will pick a game, and then pick either approved or denied. what i want it to do is start over - pick a new game, if it gets denied.

This program is a simple simulator for a dice, which has written on the 6 sides, different games, you roll the dice, and then you flip the coin, heads is yes, tails is no, if it lands on tails, you roll the dice again, untill you get one that gets heads (approved)

So i was doing this for a friend of mine. I was able to make one that is text based, the code is in post #2. But I wanted to do it all in a window.

So, my objective was to make a window with a single button "start". that when pressed, there would be a delay of about a half second, then it shows the game it picked, then another delay, and it shows wether or not that choice is approved or denied. If denied, there is another delay, then shows a new game choice, and so on and so on until one that gets approved.

I havent tried to use the delay thing from post #5 yet, but if I can implement it right, then all i need it to do is restart if denied is chosen.

And the veto it thing is something my friend kept saying, about using your veto powers to change the choice, in other words, restart...

In case you were wondering: http://en.wikipedia.org/wiki/Veto

Here's my version, slightly adapted, there's probably some new stuff in there if your new to making gui's but just ask about what you might not recognise and i'll explain, it's all pretty simple really

import random
from Tkinter import *
from time import time

def sleep(delay):
    stopTime = time()+delay
    while time() < stopTime:
        mainWindow.update()
    return

def clear():
    answerLabel.configure(text="")
    answerLabel2.configure(text="")
    mainWindow.update()

def dieSimulator():
    denied = True
    startButton.configure(text="Restart")
    while denied:
        sleep(2)
        number = random.randint(1,6)
        clear()
        if number == 1:
            #print "UNO"
            strGame = "UNO"
            answerLabel.configure(text=strGame)
        elif number == 2:
            #print "SMASH TV"
            strGame = "SMASH TV"
            answerLabel.configure(text=strGame)
        elif number == 3:
            #print "HALO"
            strGame = "HALO"
            answerLabel.configure(text=strGame)
        elif number == 4:
            #print "PDZ"
            strGame = "PDZ"
            answerLabel.configure(text=strGame)
        elif number == 5:
            #print "COD"
            strGame = "COD"
            answerLabel.configure(text=strGame)
        elif number == 6:
            #print "GRAW"
            strGame = "GRAW"
            answerLabel.configure(text=strGame)
        mainWindow.update()

        number2 = random.randint(1,2)
        restart = dieSimulator
        sleep(2)
        if number2 == 1:
            strGame2 = "Approved"
            answerLabel2.configure(text=strGame2)
            denied = False
        elif number2 == 2:
            strGame2 = "Denied"
            answerLabel2.configure(text=strGame2)
            denied = True
        mainWindow.update()
    startButton.configure(text = "start")
                
mainWindow = Tk()
    
startButton = Button(mainWindow, text="Start", command=dieSimulator, width=20)
startButton.grid(row=0, column=0)

answerLabel = Label(mainWindow, text="Game...", width=20)
answerLabel.grid(row=0, column=1)

answerLabel2 = Label(mainWindow, text="Approved/Denied...", width=20)
answerLabel2.grid(row=1, column=1)

mainWindow.mainloop()

I understood most of what you did and how it works, and the way I learn to program is by analysing why other programs can do what they do, and try to incorporate it. And if I can't find a program that does it, I ask for help, and I recently came across these forums, nothing to find here but loads of help :D

Thanks guys, and the program does EXACTLY what it is I wanted. Thanks a bunch.

no problem. glad to help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.