Search code examples
pythonwxpython

refreshing a wxPython frame/application with new values


I'm trying to make a little game with cards. The app displays the current card, but the current card changes every time you give an answer. How can I update the changed value? (There is an image and a letter that needs to be updated)
code

import wx
from random import randint

class MyFrame(wx.Frame):
    def __init__(self):
        super().__init__(parent=None, title='Card Game: Higher Lower')

        self.panel = wx.Panel(self)
        self.panel.SetBackgroundColour((255, 255, 255))
        self.my_sizer = wx.BoxSizer(wx.VERTICAL)

        self.info = wx.StaticText(self.panel, label="****Ace counts as the highest card****\n**Write 'higher' or 'lower' to answer**")
        self.infoFont = wx.Font(42, wx.DECORATIVE, wx.NORMAL, wx.NORMAL)
        self.my_sizer.Add(self.info)

        self.text_ctrl = wx.TextCtrl(self.panel)
        self.my_sizer.Add(self.text_ctrl, 0, wx.ALL | wx.EXPAND, 5)

        my_btn = wx.Button(self.panel, label="answer")
        my_btn.Bind(wx.EVT_BUTTON, self.confirm)
        self.my_sizer.Add(my_btn, 0, wx.ALL | wx.CENTER, 5)

        self.answer = None

        self.imageFile = 'Empty.png' #THIS IMAGE NEEDS TO BE CHANGED DEPENDING ON THE CARD SUIT
        self.png = wx.Image(self.imageFile, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
        self.imgbitmap = wx.StaticBitmap(self.panel, -1, self.png, (10, 5), (self.png.GetWidth(), self.png.GetHeight()))

        self.cardvalue = 't' #THIS LETTER NEED TO BE CHANGED ACCORDING TO THE VALUE OF THE CARD
        self.cardtext = wx.StaticText(self.panel, label=self.cardvalue)
        self.NumFont = wx.Font(42, wx.DECORATIVE, wx.NORMAL, wx.NORMAL)
        self.cardtext.SetFont(self.NumFont)

        self.main_sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.main_sizer.Add(self.my_sizer, 0, wx.ALL | wx.CENTER, 5)
        self.main_sizer.Add(self.imgbitmap, 0, wx.ALL | wx.CENTER, 5)
        self.main_sizer.Add(self.cardtext, 0, wx.ALL | wx.CENTER, 5)

        self.panel.SetSizerAndFit(self.main_sizer)

        self.Start = True
        self.CardUsed = 0
        self.answer = None
        self.StartGame()

        self.Show()

    def StartGame(self): #Main loop to update the data, to check the answer
        if self.Start:
            self.deck = self.GenDeck()
            self.order = self.Shuffle()
            self.Start = False
            self.firstcard = self.deck[self.order[0]]
            print(self.firstcard)
            self.cardvalue = self.firstcard.split(" ")[0]
            print(self.cardvalue)
        else:
            if self.answer:
                self.answer = self.answer.lower()

        if self.CardUsed == 52:
            return "You Won"
        self.Refresh()
        self.Update()

    def confirm(self, event):
        self.answer = self.text_ctrl.GetValue()
        self.StartGame()

    def GenDeck(self): # GENERATING A DECK OF CARDS
        global val
        val = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
        plain_deck = val * 4
        suits = ['C', 'D', 'H', 'S']
        newdeck = []
        for i in range(0, len(plain_deck)):
            suit = suits[randint(0, 3)]
            newdeck.append(f"{plain_deck[i]} of {suit}")
        return newdeck

    def Shuffle(self): # GENERATING A RANDOM ORDER FOR THE CARDS
        count = 0
        order = []
        while count < 52:
            i = randint(0, 51)
            if i not in order:
                order.append(i)
                count += 1
            else:
                continue
        return order


if __name__ == '__main__':
    app = wx.App()
    frame = MyFrame()
    app.MainLoop()

I know this code isn't fully operational (e.g. answer checking function not implemented), I just need a way to update my panel/app to the new values


Solution

  • You simply need to update the Bitmap and Refresh

    If we assume that your current card is 2.png and you type in higher, the following would update the image to 3.png. The rest I leave to you.

    def StartGame(self): #Main loop to update the data, to check the answer
        if self.Start:
            self.deck = self.GenDeck()
            self.order = self.Shuffle()
            self.Start = False
            self.firstcard = self.deck[self.order[0]]
            print(self.firstcard)
            self.cardvalue = self.firstcard.split(" ")[0]
            print(self.cardvalue)
        else:
            if self.answer:
                self.answer = self.answer.lower()
            if self.answer == "higher":
                self.imageFile = '3.png'
                self.png = wx.Image(self.imageFile, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
                self.imgbitmap.SetBitmap(wx.Bitmap(self.png))
    
        if self.CardUsed == 52:
            return "You Won"
        self.Refresh()
        self.Update()
    

    enter image description here enter image description here