I'm trying to code a desktop pet in python. For that I mainly used tkinter. I managed to create a window with two gifs which follow one another, but I have an issue with the display of the frames. As you can see in the picture, the black colour became white and sometimes it completely disappeared... In addition, for one of the gifs, two frames appear at the same time, the previous one staying instead of disappearing.
This is my code :
import random
import tkinter as tk
import pyautogui
impath = '.\\Assets\\'
x = 1400
cycle = 0
check = 0
probality=10
idle_num = list(range(0,7))
cry_num = list(range(7,11))
event_number = random.randrange(probality)
#transfer random no. to event
def event(cycle, check, event_number, x):
if event_number in idle_num:
check = 0
print('idle')
window.after(500, update, cycle, check, event_number, x)
elif event_number in cry_num:
check = 1
window.after(500, update, cycle, check, event_number, x)
# make the gif work
def gif_work(cycle, frames, event_number):
if cycle < len(frames) -1:
cycle += 1
else:
cycle = 0
event_number = random.randrange(probality)
return cycle, event_number
def update(cycle, check, event_number, x):
# idle
if check == 0:
frame = idle[cycle]
cycle, event_number = gif_work(cycle, idle, event_number)
# cry
elif check == 1:
frame = cry[cycle]
cycle, event_number = gif_work(cycle, cry, event_number)
window.geometry('842x842+' + str(x // 2) + '+50')
#window.geometry('100x100+' + str(x // 2) + '+930')
label.configure(image=frame)
window.after(1, event, cycle, check, event_number, x)
window = tk.Tk()
# call buddy's action .gif to an array
idle = [tk.PhotoImage(file=impath + 'existerGif.gif', format='gif -index %i' % (i)) for i in range(3)] # idle gif , 4 frames
cry = [tk.PhotoImage(file=impath + 'placeGif.gif' , format='gif -index %i' % (i)) for i in range(8)] # sleep gif, 8 frames
window.config(highlightbackground='white')
label = tk.Label(window)
window.overrideredirect(True)
window.wm_attributes('-transparentcolor','blue')
label.pack()
#loop the program
window.after(1,update,cycle,check,event_number,x)
window.mainloop()
Maybe it's a format problem, I did my gifs on photoshop with png images.
Anyway if someone has an idea I'll be glad to hear it ^^
Have a nice day!
Related
I am trying to build an image classifier and I need to manually assign classifications for a training dataset, so I build a file browser /media window app in tkinter to display images and assign them classifications via a button click. To iterate over the files, I am using a for loop, but I need it to pause and wait for that input. Here is my code:
def setKnownData(self):
training_sample = self.dataset.sample(frac = .1)
print(training_sample)
for i, row in training_sample.iterrows():
print(row)
global img
file = Image.open(row['ID'])
resized = file.resize((500,600))
img = ImageTk.PhotoImage(resized)
self.media_window.create_image(0,0, anchor = 'nw', image = img)
self.event_var.get()
while True:
if self.event_var.get() == 0:
print(self.event_var.get())
return
if self.event_var.get() == 1:
training_sample.loc[row]['class'] = 'cartoon'
break
elif self.event_var.get() ==2:
training_sample.loc[row]['class'] = 'photo'
break
self.event_var.set(0)
def stateSwitch(self, action):
print('state switching....')
if action == 'toon':
print(self.event_var.get())
self.event_var.set(1)
print('classification: TOON', self.event_var.get())
elif action == 'photo':
self.event_var.set(2)
print('classification: PHOTO')
I've exausted every combination of IntVar, tkinter, and for loop searches and can't find a viable solution, so I apologize if this is a repeat question. How can I pause this for loop, wait for a putton press, and then proceed to the next image in the list?
You need to shift your thinking away from pausing the loop. That's procedural programming, but GUIs are work much better as "event driven programming", where the entire program is just endlessly waiting for an event (like a button press) to happen. The means no loops, besides the tkinter mainloop. And it means making a new function for every event.
def setKnownData(self):
training_sample = self.dataset.sample(frac = .1)
print(training_sample)
self.training_sample = training_sample.iterrows()
def on_button_click(self):
i, row = next(self.training_sample)
print(row)
global img
file = Image.open(row['ID'])
resized = file.resize((500,600))
img = ImageTk.PhotoImage(resized)
self.media_window.create_image(0,0, anchor = 'nw', image = img)
self.event_var.get()
while True:
if self.event_var.get() == 0:
print(self.event_var.get())
return
if self.event_var.get() == 1:
training_sample.loc[row]['class'] = 'cartoon'
break
elif self.event_var.get() ==2:
training_sample.loc[row]['class'] = 'photo'
break
self.event_var.set(0)
I'm recreating this game for terminal in python using the curses library. Whenever a key is not pressed, stdscr.addstr() returns an error.
This seems to be because the cursor is off screen (in the bottom-right corner) during those frames, but I can't figure out why it moves at all. The canvas that I'm printing is always the same size (It consists entirely of spaces which are replaced when Apple or Basket objects are rendered). I tried decreasing the canvas size, but the cursor still goes to that corner. I also tried setting curses.leaveok(True), and the cursor seemed to be following the basket, but my logging proves that it is still going to that corner.
The file that uses curses:
import random
import time
import curses
from apple_season.basket import Basket
from apple_season.apple import Apple
from apple_season.coords import Canvas
def main(stdscr):
curses.curs_set(1) # so i can see where the cursor is
dims = [curses.COLS - 1, curses.LINES - 1] # pylint: disable=no-member
stdscr.nodelay(True)
stdscr.leaveok(True)
key=""
stdscr.clear()
canvas = Canvas(*dims)
basket = Basket(canvas)
apples = []
i = 0
def finished_apples():
if len(apples) <= 100:
return False
else:
for apple in apples:
if not apple.has_fallen:
return False
return True
while not finished_apples():
if len(apples) <= 100: # don't make more if there are already 100
# decide whether or not to create new apple (1/100 chance per frame)
num = random.randint(0, 100)
if num == 25:
apples.append(Apple(canvas))
try:
key = stdscr.getkey()
stdscr.clear()
# pick up keyboard inputs
# quit option
if str(key) == "q":
break
# right arrow
elif str(key) == "KEY_RIGHT":
basket.move('right')
# left arrow
elif str(key) == "KEY_LEFT":
basket.move('left')
except Exception:
pass
# render objects - alters canvas to display them
for apple in apples:
if apple.has_fallen:
apple.render()
else:
if '.0' not in str(i / 2): # check if i is even (drop every other frame)
apple.fall()
apple.render()
basket.render()
try:
stdscr.addstr(canvas.display)
except Exception:
pass
stdscr.refresh()
i += 1
time.sleep(0.01)
if __name__ == "__main__":
curses.wrapper(main)
(The code above runs fine, but it doesn't do anything when stdscr.addstr(canvas.display) doesn't work, which is whenever there are no keys pressed)
What's interesting is that this doesn't happen when it's just the basket or just the apples.
To see all of the code: https://github.com/lol-cubes/Terminal-Apple-Season/tree/soErrorCode.
I put the stdscr.clear() in the try and except block, which made it so that that code was only executed when a key was pressed, therefore overflowing the terminal because I was attempting to display multiple frames at once.
I'm new at programming and I cant seem to figure out how to make "konto" keep updating without it being in a while loop. Problem with the while loop is it doesn't allow new button input because it freezes the window.
I've tried making "konto" local, splitting the code into different functions, changing the loop style. Break exits the loop so "konto" doesn't update.
konto = 100
roulette_window = Tk()
def roulette(chosen_color, sats, konto):
while True:
x = randint(0, 36)
if x == 0:
green_num = x
print(green_num, 'Green')
color = ('Green')
if color and chosen_color == 'Green':
win_amount = 35 * int(sats)
elif (x % 2) == 0:
red_num = x
print(red_num, 'Red')
color = ('Red')
if color and chosen_color == 'Red':
win_amount = 2 * int(sats)
elif (x % 2) == 1:
black_num = x
color = ('Black')
print(black_num, 'Black')
if color and chosen_color == 'Black':
win_amount = 2 * int(sats)
if not color == chosen_color:
win_amount = 0
konto = konto - int(sats) + int(win_amount)
print(konto)
def bet_black():
sats = bet_input.get(1.0, END)
chosen_color = 'Black'
bet_input.delete(1.0, END)
roulette(chosen_color, sats, konto)
def bet_red():
I want to be able to call the function "roulette" once by eg. bet_black button click, and then click bet_red after one loop, while still having the updated "konto" variable.
Firstly your variable konto is a global variable and you need to indicate to python that it is. Change your method to:
def roulette(chosen_color, sats):
global konto
...
Variables in Python have scoping rules which determine where they are valid. Anytime you assign to a variable a local version of the variable is created unless you have explicitly marked it as a global variable.
Secondly, I'm not sure why you have the while True structure here. This will cause an infinite loop unless you break out of it or return.
I have created a progress bar which updates itself after getting a percentage from another function but I'm having issues getting it to trail like this ############. Instead, it just move the '#' to the right until 100% is reached. Below is my code. The reason why it's this way is because I need the percentage to come externally so that the code can be reusable. please help me.
import curses
import time
curses.initscr()
def percentage():
loading = 0
while loading < 100:
loading += 1
time.sleep(0.03)
update_progress(loading)
def update_progress(progress):
win = curses.newwin(3, 32, 3, 30)
win.border(0)
rangex = (30 / float(100)) * progress
pos = int(rangex)
display = '#'
if pos != 0:
win.addstr(1, pos, "{}".format(display))
win.refresh()
percentage()
The problem is that you call newwin() every time, discarding the old win and replacing it with a new one in the same place. That new window only gets one character added to it, with the background being blank, so you see an advancing cursor instead of a bar.
One possible solution:
import curses
import time
curses.initscr()
def percentage():
win = curses.newwin(3, 32, 3, 30)
win.border(0)
loading = 0
while loading < 100:
loading += 1
time.sleep(0.03)
update_progress(win, loading)
def update_progress(win, progress):
rangex = (30 / float(100)) * progress
pos = int(rangex)
display = '#'
if pos != 0:
win.addstr(1, pos, "{}".format(display))
win.refresh()
percentage()
curses.endwin()
(Note the addition of a call to endwin() to restore the terminal to its normal mode.)
As far as leaving it onscreen after the program finishes, that's kind of outside the scope of curses. You can't really depend on any interaction between curses and stdio, sorry.
you can just switch the pos to multiply the display #:
if pos != 0:
win.addstr(1, 1, "{}".format(display*pos))
win.refresh()
The following code is a python sprinting game. It was posted as an answer to my previous post, by #mango You have to tap 'a' and 'd' as fast as you can to run 100 meters. However, there are a few bugs...
1) If you hold down 'a' and 'd' at the same time, the game is completed as fast as possible and defeats the point of the game.
2) Like in my previous post, I would like to incorporate a scoring system into this code, however, due to my level of skill and experience with python, unfortunately, I am unable to do so, and would appreciate and suggestions.
Many Thanks
Previous code:
import msvcrt
import time
high_score = 50
name = "no-one"
while True:
distance = int(0)
print("\n--------------------------------------------------------------")
print('\n\nWelcome to the 100m sprint, tap a and d rapidly to move!')
print('* = 10m')
print("\n**Current record: " + str(high_score) + "s, by: " + name)
print('\nPress enter to start')
input()
print('Ready...')
time.sleep(1)
print('GO!')
start_time = time.time()
while distance < 100:
k1 = msvcrt.getch().decode('ASCII')
if k1 == 'a':
k2 = msvcrt.getch().decode('ASCII')
if k2 == 'd':
distance += 1
if distance == 50:
print("* You're halfway there!")
elif distance % 10 == 0:
print('*')
fin_time = time.time() - start_time
fin_time = round(fin_time,2)
print('Well done you did it in...'+str(fin_time))
if fin_time < high_score:
print("Well done you've got a new high score ")
name = input("Please enter your name : ")
This is the code that I recieved as feedback, I have made a few changes, but I am struggling witht the bugs that I listed before.
1) If you hold down 'a' and 'd' at the same time, the game is completed as fast as possible and defeats the point of the game.
2) Like in my previous post, I would like to incorporate a scoring system into this code, however, due to my level of skill and experience with python, unfortunately, I am unable to do so, and would appreciate and suggestions.
Many Thanks
# these are the modules we'll need
import sys
import tkinter as tk
class SprintGame(object):
# this represents the distance we'll allow our player to run
# NOTE: the distance is in meters
distance = 100
# this represents the stride length of the players legs
# NOTE: basically how far the player can travel in one footstep
stride = 1.5
# this represents the last key the user has pressed
lastKey = ""
# this represents wether or not the race has been completed
completed = False
# this function initiates as soon as the "sprint" variable is defined
def __init__(self):
# create the tk window
self.root = tk.Tk()
# set the tk window size
self.root.geometry('600x400')
# set the tk window title
self.root.title("Sprinting Game")
# bind the keypress event to the self.keypress handler
self.root.bind('<KeyPress>', self.keypress)
# center the window
self.centerWindow(self.root)
# insert the components
self.insertComponents()
# initial distance notice
self.output("{0}m left to go!".format(self.distance))
# start out wonderful game
self.start()
# this function centers the window
def centerWindow(self, window):
window.update_idletasks()
# get the screen width
width = window.winfo_screenwidth()
# get the screen height
height = window.winfo_screenheight()
# get the screen size
size = tuple(int(_) for _ in window.geometry().split('+') [0].split('x'))
# get the screen's dimensions
x = (width / 2) - (size[0] / 2)
y = (height / 2) - (size[1] / 2)
# set the geometry
window.geometry("%dx%d+%d+%d" % (size + (x, y)))
# this function replaces the old text in the textbox with new text
def output(self, text = ""):
self.text.delete('1.0', tk.END)
self.text.insert("end", text)
# this function handles key presses inside the tkinter window
def keypress(self, event):
# get the key and pass it over to self.logic
self.logic(event.char)
# this function handles game logic
def logic(self, key):
# convert key to a lower case string
key = str(key).lower()
# let us know how far we've got left
if key == "l":
self.output("{0}m left to go!".format(self.distance))
# restart the race
if key == "r":
# clear the output box
self.text.delete('1.0', tk.END)
# reset the distance
self.distance = 100
# reset the stride
self.stride = 1.5
# reset the last key
self.lastKey = ""
# set race completed to false
self.completed = False
# output restart notice
self.output("The Race Has Been Restarted.")
# don't bother with logic if race is completed
if self.completed == True:
return False
# check if distance is less than or equal to zero (meaning the race is over)
if self.distance <= 0:
# set the "self.completed" variable to True
self.completed = True
# let us know we've completed the race
self.output("Well done, you've completed the race!")
# return true to stop the rest of the logic
return True
# convert the key to lower case
key = key.lower()
# this is the quit function
if key == "q":
# lights out...
sys.exit(0)
# check if the key is a
if key == "a":
# set the last key to a so that we can decrement the "distance"
# variable if it is pressed next
self.lastKey = "a"
# only bother with "d" keypresses if the last key was "a"
if self.lastKey == "a":
# capture the "d" keypress
if key == "d":
# decrement the "distance" variable
self.distance -= self.stride
# let us know how far into the game we are
self.output("{0}m left to go!".format(self.distance))
# this function inserts the components into the window
def insertComponents(self):
# this component contains all of our output
self.text = tk.Text(self.root, background='#d6d167', foreground='#222222', font=('Comic Sans MS', 12))
# lets insert out text component
self.text.pack()
# this function opens the window and starts the game
def start(self):
self.root.mainloop()
# create a new instance of the game
Game = SprintGame()