0

I have a script that I am trying to design to initialize a GUI with a default image and a button. When the button is pressed, it calls the getRandomCard function and is meant to replace the current image with a random file (ex: 1c, 2s, 5d, etc.) They are GIF files of playing cards.

I am working on getting the first image (default placeholder) and the button to appear when I run the program. I am currently getting an empty window with no widgets inside of it. What part of code am I missing, or what is not written properly?

import random
from tkinter import *

class randomCards(object):
    def __init__(self, rank, suit):
        suitlst = ["s", "h", "d", "c"]
        self.rank = random.randint(1,13)
        self.suit = random.choice(suitlst)
    def getRandomCard(self):
        fileString = "DECK/" + str(self.rank) + str(self.suit)
        userDisplay.imgLabel = Label(window, image = fileString).pack(side= "left")
        userDisplay.imgLabel.pack()

class userDisplay(Frame):
    def __init__(self, window):
        self.master = window
        window.title = "Random Images"
        self.imgLabel = Label(window, image = "DECK/b.gif")
        self.imgLabel.pack(side = "left")
        self.cardButton = Button(window, text = 'Show Random Card', command=lambda : randomCards.getRandomCard(self))
        self.cardButton.pack()

def main():
    root = Tk()
    interface = userDisplay(root)

main()

1 Answer 1

1

I am working on getting the first image (default placeholder) and the button to appear when I run the program. I am currently getting an empty window with no widgets inside of it. What part of code am I missing, or what is not written properly?

You never call pack, place, or grid on the instance of userDisplay. Since it is an instance of Frame, it will only be visible once it has been added to the screen with one of those geometry managers.

Assuming that you want userDisplay to fill the entire window, add this inside of main:

interface.pack(fill="both", expand=True)
Sign up to request clarification or add additional context in comments.

2 Comments

I added that and now it shows the button and a transparent space where the image would be. Assuming that the directory is right, am I also missing something to display the GIF file?
@BenRoux: the image option does not take a pathname. There are plenty of examples on the internet of how to use images with labels.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.