用新值刷新 wxPython frame/application

refreshing a wxPython frame/application with new values

我正在尝试用卡片制作一个小游戏。该应用程序显示当前卡片,但每次您给出答案时当前卡片都会更改。如何更新更改后的值? (有一张图片和一封信需要更新)
代码

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()

我知道此代码未完全运行(例如未实现答案检查功能),我只需要一种方法将我的 panel/app 更新为新值

您只需要更新位图和 Refresh

如果我们假设您当前的卡片是 2.png 并且您输入 higher,则以下会将图像更新为 3.png。剩下的交给你了。

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()