Using Tkinter and tkmacosx.
I am making a dice roller and when the button is pressed successively multiple times, it would run several times which isn't what I would like the program to do. Here is the code:
def rolled_lbl (event):
global roll_lbl_txt
sleep(0.2)
roll_lbl_txt = "You Rolled a" # show at first
rolling_lbl.place(x=345, y=200)
for x in range(4): # repeating 4 times as the first time the label stays the same
rolling_lbl.config(text = (roll_lbl_txt)) # has to be implemented because of the changing variable
rolling_lbl.update() # update the label on screen
sleep(0.25) # wait for a better effect
roll_lbl_txt += "." # adds the dot
# hides previous dice and rolled label
def hide_previous(event):
rolling_lbl.place_forget() # place.forget is used as i placed the objects in these positions so place.forget removes them
d1.place_forget() # dice image 1
d2.place_forget() # dice image 2
d3.place_forget() # dice image 3
total.place_forget() # total value
window.update() # refreshes window
sleep(0.2) # creates better effect so it isnt rushing
# dice value event
# randomiser uses the number of sides as a maximum value
# this randomised number is then placed into a string text which has the location of the dice images
def roll_dice():
global dice_sides # to get values from dice sides dropdown
global dice_value # to tell the total value codes what the value was
dice_value = random.randint(1,(dice_sides))
dice_img= "Resources/Dice_imgs/dice_img_"+str(dice_value) +".png" #variable with image location must use string as location needs to be in string format
return dice_img #returns the location to the image processing function
#event which updates dice images on screen
def update_dice(event):
global updated_picture
global updated_picture_2
global updated_picture_3
global value_d1 # for totals function
global value_d2 # for totals function
global value_d3 # for totals function
value_d2 = 0 # must be set to zero otherwise when reducing number of dice, value stays
value_d3 = 0 # must be set to zero otherwise when reducing number of dice, value stays
if no_dice == 1: # if there is only 1 dice running - from number of dice dropdown
dice_img = roll_dice() # gets location from randomiser
updated_picture = ImageTk.PhotoImage(Image.open(dice_img)) # opens location and gets image
d1.place(x=312 ,y=255) # puts on screen
d1.configure(image = updated_picture) # ensures image is updated picture
value_d1 = dice_value # tells total the first dice value - must be done because Dice_value is used for dice 2&3 as well
elif no_dice == 2: # if there is 2 dice running from number of dice dropdown - reuses same code from above for the first but changes location, new variables for second
dice_img = roll_dice() # gets location
updated_picture = ImageTk.PhotoImage(Image.open(dice_img)) #runs and opens image
d1.place(x=110 ,y=255) # places location of image on screen
d1.configure(image = updated_picture) # puts image in location
value_d1 = dice_value # tells total value of dice one
dice_img = roll_dice() # gets location for dice two - dice one has already been inputed so variable can be reused
updated_picture_2 = ImageTk.PhotoImage(Image.open(dice_img)) # loads image from location
d2.place(x=525 ,y=255) # places on righthand side of screen
d2.configure(image = updated_picture_2) # updates image
value_d2 = dice_value # tells total value of dice two
elif no_dice == 3: # if there is 2 dice running from number of dice dropdown - reuses same code from above for the first and second but changes location, new variables for third
dice_img = roll_dice() # gets location of dice 1
updated_picture = ImageTk.PhotoImage(Image.open(dice_img)) # loads image
d1.place(x=46 ,y=255) # puts location on far left of screen
d1.configure(image = updated_picture) # updates image to be new loaded one
value_d1 = dice_value # tells total the value of dice one
dice_img = roll_dice() # gets location of dice 2
updated_picture_2 = ImageTk.PhotoImage(Image.open(dice_img)) # loads image
d2.place(x=321 ,y=255) # places in middle of screen
d2.configure(image = updated_picture_2) # updates image
value_d2 = dice_value # tells total the value of dice two
dice_img = roll_dice() # gets location of dice 3
updated_picture_3 = ImageTk.PhotoImage(Image.open(dice_img)) # loads image
d3.place(x=596 ,y=255) # places on right of screen
d3.configure(image = updated_picture_3) # updates image
value_d3 = dice_value # tells total value of dice three
# Event which runs Total at bottom of screen
def total_lbl (event):
total_txt = "The total is: " # original text
dice_total = (value_d1 + value_d2 + value_d3) # add values of dice one two (if available) and 3 (if available)
total_txt += str(dice_total) # adds total of dice to original text
total.config(text=(total_txt)) # updates label to have text
total.update()
total.place(x=350, y=535)
roll=Button(window, text="Roll the Dice", background="red", fg="white", activebackground='#00FF00',font=("Calibri",(30)), width =180, height = 55, borderless=1)
roll.place(x=350, y=120)
# when the button is clicked:
# happens in order shown
roll.bind('<Button-1>', hide_previous, add="+") # commands hide_previous function to hide old objects
roll.bind('<Button-1>', rolled_lbl, add="+") # commands rolled_lbl to display you rolled a
roll.bind('<Button-1>', update_dice, add="+") # commands update_dice to update dice images and display on screen
roll.bind('<Button-1>', total_lbl, add="+") # commands total value to display on screen
Is there a way to stop the button from running the functions whilst it is going? I don't want to use disable, however.
Could I create a variable that will only let the code work once it has begun and leave the button useless until it has finished all of the 4 functions?
Related
I'm making a simulation of a traffic light and a car. When the light turns green, the car should move. (I'm aware that actual stoplights don't jump from green to red, or from red to yellow, but...just go with it). The program takes input from the user as to how long the stoplight should loop; it starts by remaining red for 3 seconds and then looping through the other colors.
So my problem is that when I try to move the car (represented by a Rectangle object here) and wheels (two Circle objects), the graphics window appears to be creating an entirely new rectangle, despite the fact that I haven't called the clone() method or anything anywhere in my program. Try running the program for 8+ seconds to see.
Why is the car rectangle making copies of itself instead of just moving the original?
Code below:
import sys
from graphics import *
import time
def drawCar(win):
car = Rectangle(Point(0,510), Point(100,555))
car.setFill(color_rgb(255,0,0))
wheel1 = Circle(Point(15,565),10)
wheel2 = Circle(Point(75,565),10)
wheel1.setFill(color_rgb(0,0,0))
wheel2.setFill(color_rgb(0,0,0))
car.draw(win)
wheel1.draw(win)
wheel2.draw(win)
def drawTrack(win):
rect = Rectangle(Point(0,575),Point(500,600))
rect.setFill(color_rgb(0,0,0))
rect.draw(win)
drawCar(win)
def loop(x):
# opens and titles a graphics window
win = GraphWin("Traffic Light", 500,600)
# creates 3 black circles for the window
red = Circle(Point(250,100),80)
yellow = Circle(Point(250,260),80)
green = Circle(Point(250,420),80)
# draw the car and track
drawTrack(win)
corner1 = Point(0,510)
corner2 = Point(100,555)
car = Rectangle(corner1,corner2)
car.setFill(color_rgb(255,0,0))
wheel1 = Circle(Point(15,565),10)
wheel2 = Circle(Point(75,565),10)
wheel1.setFill(color_rgb(0,0,0))
wheel2.setFill(color_rgb(0,0,0))
car.draw(win)
wheel1.draw(win)
wheel2.draw(win)
# sets default colors of the circles
red.setFill(color_rgb(255,0,0))
yellow.setFill(color_rgb(0,0,0))
green.setFill(color_rgb(0,0,0))
red.draw(win)
yellow.draw(win)
green.draw(win)
redCount = 1 # red light counter is automatically set to 1 because it starts with red by default
yelCount = 0 # yellow and green light counters are set to 0
greenCount = 0
redSet = True
yellowSet = False
greenSet = False
start = time.time()
end = time.time() + x
time.sleep(2) # wait 2 seconds while showing the red light (since the loop waits 1 additional second on the red), then begin the color-changing
while (time.time() - start < end):
if(time.time() + 1 > end): # if the time it will take to rest on the next light will make it go overtime
break # then stop
if redSet:
print("Red, changing to yellow")
red.setFill(color_rgb(0,0,0))
yellow.setFill(color_rgb(255,255,0))
yelCount += 1
redSet = False
yellowSet = True
time.sleep(1) # hold the yellow light for 1 second
elif yellowSet:
yellow.setFill(color_rgb(0,0,0))
green.setFill(color_rgb(0,255,0))
greenCount += 1
yellowSet = False
greenSet = True
time.sleep(1) # hold green light for 1 second
#corner1.move(60,0)
#corner2.move(60,0)
car.move(60,0)
wheel1.move(60,0)
wheel2.move(60,0)
print("Corners moved")
elif greenSet:
green.setFill(color_rgb(0,0,0))
red.setFill(color_rgb(255,0,0))
greenSet = False
redSet = True
time.sleep(1)
redCount += 1
else:
break
print("Red light hit ", redCount)
print("Yellow light hit ", yelCount)
print("Green light hit ", greenCount)
# if the user clicks anywhere in the window
win.getMouse()
# then we close the window
win.close()
def main():
# prompts the user for a time limit
x = float(input("How many seconds do you want the traffic light simulation to last? "))
# prints confirmation
print("Starting the loop for ", x, " seconds:")
# begins the loop
loop(x)
main()
The problem is because you're drawing the car (and wheels) twice.
Fortunately the fix to prevent that is simple:
def drawTrack(win):
rect = Rectangle(Point(0,575),Point(500,600))
rect.setFill(color_rgb(0,0,0))
rect.draw(win)
# drawCar(win) # Don't do this.
Note: After doing this, there will be no calls at all the function drawCar() so you could remove it. Another alternative would be to leave it in and replace the lines of code in the initialization part of the loop() function that do the same thing with a call to it (thereby making the code more modular).
I participated in a local programming competition yesterday, where I was put on a team of four with members of varying skill levels. We were trying to make a text-based Zork style game, but since UI was one of the categories I was put on creating a GUI which I have no experience with in python. I used Tkinter and got everything mostly working inside the GUI itself, but can't figure out how to get the GUI to interact with the rest of the code.
I'm just including the relevant bits here, partially because the rest of the code is quite a mess, with no comments and lots of syntax errors, but I can post it if needed. Here's the code:
def pinput():
global e
player_input = e.get()
print(player_input)
def progress():
hallway_1()
def hallway_1():
global player_input
global prin
global e
prin.set("You are in a hallway. Yellow lockers line the sides of all walls and you see three doors. One leads into a Library, one to a History classroom, and one to a Math room. You can 1.) Search the hallway or 2.) Procede to one of the rooms.")
while player_input != "1" and player_input != "2": #Best guess at a way to get the code to wait for input, actually just causes code to freeze
pass
if player_input == "1": #Even if 1 is in the entry box beforehand, it never gets here
prin.set("You search through the lockers and find 1 health potion.\n Would you like to add it to your inventory? 1.) Yes 2.) No")
player_input = "999"
def player_attack():
global enemy_hp
enemy_hp -= player_strength
prin.set("You just attacked the " + enemy_name + "!")
def player_block():
global block
prin.set("You predict that your enemy is going to attack and decide to block")
block = 1
def player_use():
global player_item_use
global item_name
player_item_use = 100
prin.set("You decide to use one of your items. What do you use?")
while player_turn_input != 0 or 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9 or 10 or 11 or 12 or 13 or 14 or 15 or 16 or 17 or 18 or 19 or 20 or 21 or 22 or 23 or 24 or 25 or 26:
player_item_use = e.get()
time.sleep(1)
item_name = Inventory[player_item_use]
item(item_name)
def run_away():
global player_hp
global lost_item_1
global lost_item_2
prin.set("You decide to run away from the " + enemy_name + "!\nThis causes you to lose 20 health points and two inventory items.")
player_hp -= 20
if backpack == True:
lost_item_1 = random.randint(1,27)
lost_item_2 = random.randint(1,27)
else:
lost_item_1 = random.randint(1,9)
lost_item_2 = random.randint(1,9)
RemoveInventory(lost_item_1)
RemoveInventory(lost_item_2)
library()
top = tk.Tk()
prin = tk.StringVar()
#path = "smalllibrary.png"
#tkimage = ImageTk.PhotoImage(Image.open(path))
#displayimage = tk.Label(top, image=tkimage).grid(row=0)
inv = tk.Text(top, width="24")
inv.grid(row=0, column=1)
for x in Inventory:
inv.insert("end", str(x) + str(invnum) + '\n')
invnum += 1
healthbar = tk.Label(top, text=("Health: " + str(player_hp))).grid(row=1, column=1)
text = tk.Label(top, bg="black", fg="white", textvariable=prin, justify="left", cursor="box_spiral").grid(row=1, sticky="w")
e = tk.Entry(top)
e.grid(row=2)
e.focus_set()
Attack = tk.Button(top, text ="Attack", activebackground="red", width="10", command = player_attack).grid(row=4, sticky="e")
Block = tk.Button(top, text ="Block", activebackground="red", width="10", command = player_block).grid(row=5, sticky="e")
Use = tk.Button(top, text ="Use Item", activebackground="red", width="10", command = player_use).grid(row=4, column=1, sticky="w")
Run = tk.Button(top, text ="Run", activebackground="red", width="10", command = run_away).grid(row=5, column=1, sticky="w")
Enter = tk.Button(top,text='Enter',command=pinput).grid(row=3)
Progress = tk.Button(top, text="Progress", activebackground="green",
prin.set("REDACTED")#This had my team's full names
top.mainloop()
So, there are a few problems. I added the "progress" button for testing purposes, ideally the game would start in Hallway 1. However, I couldn't figure out where to put the hallway_1() function for this to work. Before the top.mainloop() and the GUI wouldn't open. After it, and e.get() threw TCL errors.
So, I bound it to a button. However, now the code keeps getting stuck in the "while" loop even if 1 is inputted into the entry box before the code runs. I'm perplexed by this because Visual Studio reports player_input as being '1' in the Autos box, but it keeps running through the loop anyway.
You can't do a while loop like that in a GUI event handler. Until you return from the handler function, the GUI doesn't get to update its display, accept user input, or do anything else, which means player_input will never change once you get there. A handler function needs to do one thing, set up anything needed for future handlers, and return immediately.
It's hard to get your head around thinking in terms of the event loop and handler callbacks the first time you write a GUI, but it's absolutely essential; until you do, things seem bizarre and arbitrary.
So, the answer isn't to "wait for input", but to set up a handler that fires when that input is ready. (You can instead set up a handler on an after function so it fires every N milliseconds instead of firing on input, but that usually just makes things more complicated.)
One way to do this is to have the "Progress" button trigger "the next step", and keep track of some state that lets you determine what "the next step" is. An in fact, you already have that state: it's whether the player has already given correct input or not. So:
def pinput():
global player_input
player_input = e.get()
print(player_input)
def progress():
pinput()
if player_input != "1" and player_input != "2":
hallway_1_enter()
else:
hallway_1_doit()
def hallway_1_enter():
global player_input
global prin
global e
prin.set("You are in a hallway. Yellow lockers line the sides of all walls and you see three doors. One leads into a Library, one to a History classroom, and one to a Math room. You can 1.) Search the hallway or 2.) Procede to one of the rooms.")
def hallway_1_doit():
if player_input == "1": #Even if 1 is in the entry box beforehand, it never gets here
# etc.
This is obviously a bit of a clunky design, but it's the smallest change to your existing design that gets you past the first step. Once you get the hang of it, you should be able to improve it from there.
Also notice the global player_input I put in pinput. Your existing function didn't have that, so it was just creating a local variable with the same name, which then goes away immediately; the global never gets changed. Any function that wants to assign to a global needs the global statement.
I am building a rudimentary python program for tracking bullet fire in my local Rogue Trader campaign. I hate writing - erasing - rewriting on my sheet leaving it smudged and gross. This gives me an excuse to practice my coding skills. Eventually going to have it save values to a file and then read them upon start up, but that's in the future.
I am letting it ask for what guns I have, setting a clipSize for said gun, and then create a button that references each gun. Upon pressing the button, fireGun is supposed to take the value of the gun shot corresponding to which button is pressed. However, the way it runs currently, all guns fire from the same ammo amount which is the last 'clipSize' entered.
I need each button to track its own variable to update the correct dictionary reference upon fireGun.
from tkinter import *
addGuns = 'true'
gunList = {}
while (addGuns == 'true'):
newGun = input("What is the name of your gun? ")
clipSize = int(input("What is its clip size? "))
gunList[newGun] = clipSize
gunCheck = input("Done adding guns? ")
if (gunCheck == 'yes'):
addGuns = 'false'
root = Tk()
root.title("Pew Pew")
def fireGun(x):
startingAmmo = gunList[x]
endingAmmo = startingAmmo - 1
gunList[x] = endingAmmo
print(gunList[x])
return
for gun in gunList:
button = Button(root, text = gun, command = lambda name = gun:fireGun(gun))
button.pack()
root.mainloop()
Use partial to send parameters to a function with a command= call. You could also use an Entry to get the gun info, and one label for each gun with the name and updated ammo amount on each label. A very sloppy example (I have to go to work).
from tkinter import *
from functools import partial
gunCheck="no"
gunList = {}
while gunCheck != 'yes':
newGun = input("What is the name of your gun? ")
clipSize = int(input("What is its clip size? "))
gunList[newGun] = int(clipSize)
gunCheck = input("----->Done adding guns? ")
## if (gunCheck == 'yes'):
## addGuns = 'false'
root = Tk()
root.title("Pew Pew")
def fireGun(x):
startingAmmo = gunList[x]
endingAmmo = startingAmmo - 1
gunList[x] = endingAmmo
print(gunList[x])
label_list[x].config(text=x + "-->" + str(endingAmmo))
return
label_list={}
fr=Frame(root)
fr.pack(side="top")
for gun in gunList:
lab=Label(fr, text="%s --> %d" %(gun, gunList[gun]))
lab.pack(side=TOP)
label_list[gun]=lab
button = Button(root, text = gun, command = partial(fireGun, gun))
button.pack()
root.mainloop()
So I have a currency which is increasing (that system is working fine). The first part updates the label every 100 ms. I have another button which triggers the second function which is supposed to clear the labels from the first. It sets home_status equal to 0 which should in theory run Money.place_forget() to clear the code. I have tested each part individually and it works but when I put the clears inside the elif statement it doesn't. It does not give me any errors, it just simply doesn't do anything (it does print END OF UPDATE HOME so the elif is triggered).
Any suggestions?
def updatehome(self):
print("UPDATE HOME")
global buy_button, home_status, currency
MoneyLabel = Label(self, text = "Money: ")
MoneyLabel.place(x = 5, y = 70)
Money = Label(self, text=currency)
Money.place(x = 50, y = 70)
if (home_status == 1):
self.after(100, self.updatehome)
elif (home_status == 0):
print("END OF UPDATE HOME")
Money.place_forget()
MoneyLabel.place_forget()
def clearhome(self):
print("CLEAR HOME")
global home_status
home_status = 0
you are creating ten labels every second, all stacked on top of each other, but you are only deleting the very last label you create.
I am writing a rock, paper, scissors game in Python but my code doesn't work as it should. I'm new to Python so please let me know if my code isn't formatted corectly. The game runs fine, assuming you enter one of the already existing answers. However, if you enter one that is different, the code seems to loop randomly after the 'end()' function is executed.
Here is my code:
# imports needed files
from random import randint
import time
# creates a function that ends the game
def end(cpuScore,playerScore):
time.sleep(1)
cont = input("Would you like to play again? (y or n)\n")
if cont=="y":
time.sleep(1)
start()
else:
print("Well... That's a bit rude.")
# creates a function to play the game
def rps(cpuScore,playerScore,num):
# loops code 3 times (unless 'num' is different)
for x in range(num):
num-=1
# creates options
options = ["rock","paper","scissors"]
# picks a random choice for cpu
cpu = options[randint(0,2)]
# asks the player to choose
player = input("rock, paper or scissors?\n")
# why not gun?
if player=="gun":
result = "w"
elif player==cpu:
result = "d"
elif player=="rock":
if cpu=="paper":
result = "l"
if cpu=="scissors":
result = "w"
elif player=="paper":
if cpu=="scissors":
result = "l"
if cpu=="rock":
result = "w"
elif player=="scissors":
if cpu=="rock":
result = "l"
if cpu=="paper":
result = "w"
# if they choose something other than rock, paper, scissors or gun
else:
print("That's an invalid input!")
# adds one to num so that this round is not counted as one of the 3
num+=1
# plays the game again with the amount of rounds remaining
rps(cpuScore,playerScore,num)
# tells the player how they did
if result=="w":
playerScore+=1
time.sleep(1)
print("Fine! You win! Your silly " + player + " beat my " + cpu + "!!!")
if result=="l":
cpuScore+=1
time.sleep(1)
print("Ha! Sucker!! My epic " + cpu + " smashed your measly " + player + "!!!")
if result=="d":
time.sleep(1)
print("Ah! We drew by both choosing %s! Like they say, great minds think alike!" % cpu)
# announces the scores
print("You are on %s and the computer is on %s!" % (playerScore,cpuScore))
# ends the game after 3 rounds
end(cpuScore,playerScore)
# creates the funtion that sets the variables and starts the game
def start():
result=""
cont=""
cpuScore=0
playerScore=0
rps(cpuScore,playerScore,3)
# begins the game
start()
Thanks
Basically your rps function loops num times, with num = 3 initially. If the user enters an incorrect input, you call back the function, which starts the whole process again, in a new context, for num+1 times.
Thus if you answer wrong the first time you have at least six games to play: four new added and the two initial ones you didn't try to play.
My advice try first to do a program that do one and only one iteration of the rock-paper-scissor game. Adding more iteration is a simple fact of adding a global loop.