Problem with linking images from computer to Python Tkinter file? - python

I'm making a blackjack game using Python, and the GUI with Tkinter.
I have this so far. It doesn't output the cards yet.
Without cards
I have then used this bit of code to try and link the photos to the file:
code, and this is the output:
with cards.
This is where the problems start.
it is really inconsistent. I have to try to open the file several times in order for the game window to actually open.
When it does finally open, it doesn't work properly. When I click the 'hit' button, I can see that the number of cards left in the deck is going down ( top left corner of the screenshots ), but the photos don't change ; they just remain as the ones in the screenshot.
If I spam click the shuffle button, one of them might change to another image, but they mostly remain the same.
If I remove those lines of code linking the images, it works properly and as expected ( first screenshot ). The cards actually change when 'hit' and 'shuffle' are clicked, although its just the name of the card and not the image.
I'm very new to Tkinter, and this is my first program using it. Can someone please suggest any changes/improvements I can make so that the images of the cards change everytime 'hit' and 'shuffle' are clicked, and also explain briefly why its that way? Any and all help will be greatly appreciated.
Thanks a lot

Related

Kivy sometimes does not show widgets and images

Does anyone know why kivy sometimes does not show new widgets on the screen. I have the identical code for several screens in an app writtin in .kv-language. It works fine in all screen but one screen wont show my the widgets and images.
I had the same problem a view times now, sometimes it works after a while but it is starting to get annoying since I can not continue coding before my widgets decide to show up.
it often helps to just change the name of the screen and all its mentions. I still do not get what is going on, so if somebody has an answer please tell me.

Cycled images in linux/gtk program suddenly stop displaying

I have an extremely simple application for a Raspberry Pi. (It's an educational kiosk for a children's museum if anyone cares.) In python, I have an infinite loop in a thread reading a line from a serial port. Based on the input, I display one of 14 different jpg images. I am not putting all the code here, but it's a very bare-bones GDK application. I have an Arduino feeding the serial port the information to cycle through all the images for debug purposes. In response to the input, I do the following:
self.CurrentImage.set_from_file("image.jpg") # the name here is one of 14
Not to anyone's great surprise, this works. But as I let the Arduino hammer at the input, the screen would randomly show a white image and nothing again after. I checked the standard out window and the data was still coming and the images still being read. And when I say random I mean that at some point in the input-and-display process, it stops displaying. There are no errors being reported. Sometimes I might get 4-5 images in sequence before it dies, or I might make it through the list twice. It's simply not deterministic. My mind wandered to thinking maybe I'm not clearing first and having a memory leak. I made the following amendment:
self.CurrentImage.clear()
self.CurrentImage.set_from_file("image.jpg")
The problem persisted. I decided to scrap the method and go for something that didn't involve reloading images. At startup I created a separate GTK Image widget for each file. Then in response to the input data, I did this:
self.CurrentImage.hide()
self.CurrentImage = self.AlphaImage # or one of the other 13 Images I created
self.CurrentImage.show()
The nice thing about this method is that the image displays much faster. The first method had the screen briefly go white as the image was loaded. However, once again, after a random number of image switches, the window goes white. Diagnostic output shows that the loop is happily reading data and selecting images.
In the original version where I loaded images as needed, there was exactly one widget on the window. So it's not possible that another widget is covering it. The second version has an Image widget for each jpg file. If one is covering another, I should still at least see that image.
I'm good at thinking outside the box, but I admit that Linux is a weak area for me. Nothing is occurring to me to try to make this work. I'd whinge that I'm under time pressure here and children will be disappointed... but it was supposed to be done before Christmas and I only got the final art yesterday. That reminds me that there's one final note and the reason I thought my first method was failing: I created temporary graphics of my own that was one word of black text on a white background. Those images displayed without problem until the screensaver kicked in.
I'm open to any suggestion as to how to track this down and fix it.
Thanks to Sylvester, I figured this one out. The problem isn't how I was updating the images, it was where I was doing it. In the thread catching the serial input was not the place to do it. I reduced the thread to simply reading the line, then did the following:
GLib.idle_add( self.updateImage, lineInput )
then in the function updateImage I did the business logic of selecting the correct image and updating. Problem solved.

button click in python turtle module

I am very new to this so please do not judge if my question seems too obvious.
so I am trying to make a game in the python turtle module and I would like to have different buttons for loading and quitting. I have images of these buttons and I am making these images my turtle object by using .addshape() and .shape() methods. The next step for me is to make sure that when I click any of these buttons I get the desired outcome. My issue is that I cannot figure out how to make that happen i.e how can I make my program do the thing when the mouse clicks within a certain image? I have tried using turtle.onclick() method, but I guess I am not able to utilize that either. Any help would be much appreciated.

Template matching results when the template is not on the original image

For testing purposes I'm building a bot that clicks on buttons in the screen. I have a folder with all the possible buttons that needs to be pressed and the application being tested only has one button shown at a time. There is no situations where two or more buttons appear together on the screen.
My approach is to take a screenshot every few seconds and loop through all the possible buttons and try to find them on the screenshot. If the button is found the bot clicks the center of the button.
My issue is that even if the button is not present on the screenshot the template algorithm I'm using returns a false positive somewhere in the screen. Is there any way to make sure no false positives are returned unless the button exists on the image?
I'm using python with numpy and skimage. For the template matching I'm using skimage.feature.match_template.
I already tried with opencv using SIFT and all those things with not much success and a lot of issues with opencv itself.
P.S.: If you think there is a better way to solve the problem itself (testing an app by pressing buttons) they are welcome too.
Cheers
Edit 1:
These are the images:
Edit 2:
The output of the script (false positive)
As I realised later on, the only way to make sure the detected image is indeed the one you are searching for is to compute the difference between the detected one and the template and use a threshold to allow for imperfections.

Python Tkinter get image of whats behind a window

Well i am creating a utility for my own needs, that being to help improve my reading on the computer. What i need to do is create a tool which can add a colour filter to a section of the screen, as well as add zoom. The first thing was easy to do, i set the attributes to add alpha, topmost and gave a frame a set bg colour.
What i dont know how to do is the zoom. I was thinking of getting a screen capture cut the selected bit apply image transformation then render it in the window. Of course the utility window will also be drawn and obscure the screen capture. I dont know tkinter well enough and i really want to avoid doing GDI work, but if someone has a solution with it i dont mind using it. Clearly referencing GDI, im making it to run on windows.
Optional: Something i have a work around for but would be nice to have is the ability to click through a window, but i doubt i can do that with tkinter.
EDIT: Since im getting negative votes, and i don't know why please help me improve with a comment.
I'm not looking for someone to do the work for me i just want a hint on what might be the right way to do this. With the main problem being how to get a image for whats underneath the Tkinter window so i can process it.

Categories