【问题标题】:How to make python update or refresh??? (wxpython)如何使python更新或刷新??? (wxpython)
【发布时间】:2015-03-03 13:19:12
【问题描述】:

在哪里以及如何让它更新或刷新???

如果我按下底部按钮(button1, button2, button3),我想更改上下文、位置等。

但我不知道怎么做。你能帮我一下吗???

代码如下:

import wx


class mainFrame(wx.Frame):
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, "refresh&update", size=(900,600))

        self.count = 0

        if self.count == 0:
            self.text = wx.TextCtrl(self,  pos=(0, 0), size=(100, 100))
            self.text.AppendText('00000000')

            self.text1 = wx.TextCtrl(self,  pos=(100, 100), size=(100, 100))
            self.text1.AppendText('00000000')

            self.text2 = wx.TextCtrl(self,  pos=(170, 250), size=(100, 100))
            self.text2.AppendText('00000000')

        if self.count == 1:
            self.text3 = wx.TextCtrl(self,  pos=(100, 50), size=(100, 100))
            self.text3.AppendText('1111111')

            buttonl = wx.Button(self, label="1111111", pos=(450,100), size=(100,100))
            button2 = wx.Button(self, label="1111111", pos=(250,100), size=(100,100))

        if self.count == 2:
            self.text6 = wx.TextCtrl(self,  pos=(0, 0), size=(100, 100))
            self.text6.AppendText('22222222')

            self.text = wx.TextCtrl(self,  pos=(100, 100), size=(100, 100))
            self.text.AppendText('22222222')

            button3 = wx.Button(self, label="22", pos=(250,170), size=(100,100))
            button4 = wx.Button(self, label="22", pos=(350,170), size=(100,100))



        button1 = wx.Button(self, label="count0", pos=(150,450), size=(200,100))
        button1.Bind(wx.EVT_BUTTON, self.OnB1)


        button2 = wx.Button(self, label="count1", pos=(400,450), size=(200,100))
        button2.Bind(wx.EVT_BUTTON, self.OnB2)


        button3 = wx.Button(self, label="count2", pos=(650,450), size=(200,100))
        button3.Bind(wx.EVT_BUTTON, self.OnB3)



    def OnB1(self, event):
        self.count = 0

    def OnB2(self, event):
        self.count = 1

    def OnB3(self, event):
        self.count = 2


app = wx.App()
frame = mainFrame(None, id = -1).Show()
app.MainLoop()




    ############################# error////////////////////////////////////
                self.counts = [
                    [ #count =0
                        image=wx.Image(u"aaa.jpg",wx.BITMAP_TYPE_ANY),
                        self.bitmap_1 = wx.StaticBitmap(self, wx.ID_ANY, wx.BitmapFromImage(image.Rescale(350, 350)), (490,420)),
                        self.bitmap_1.Bind(wx.EVT_LEFT_DOWN, self.onClick,
                    ],
                    [ #count = 1
                        button_cancel = wx.Button(self, label="취소", pos=(490,550), size=(300,150)),
                        button_cancel.SetFont(wx.Font(40, wx.ROMAN, wx.SLANT, wx.BOLD, 0, "")),
                    ],
                ]
##################################

# -*- coding: utf-8 -*-

import wx

def BoundButton(*args,**kwargs):
    callback = kwargs.pop("callback",lambda e:None)
    btn = wx.Button(*args,**kwargs)
    btn.Bind(wx.EVT_BUTTON,callback)
    return btn

def hide_all(itm):
    if isinstance(itm,(list,tuple)):
        for i in itm:
            hide_all(i)
    else:
        itm.Hide()

def show_all(itm):
    if isinstance(itm,(list,tuple)):
        for i in itm:
            show_all(i)
    else:
        itm.Show()

class mainFrame(wx.Frame):
    def __init__(self):

        wx.Frame.__init__(self, None, -1, "refresh&update", size=(900,600))

        button_cancel = wx.Button(self, label="B1", pos=(100,400), size=(100,150))
        button_cancel.Bind(wx.EVT_BUTTON, self.OnB1)

        button_cancel = wx.Button(self, label="B2", pos=(300,400), size=(100,150))
        button_cancel.Bind(wx.EVT_BUTTON, self.OnB2)

        button_cancel = wx.Button(self, label="B3", pos=(500,400), size=(100,150))
        button_cancel.Bind(wx.EVT_BUTTON, self.OnB3)


        self.count = 0

        self.counts = [
            [ #count =0
                BoundButton(self, label="count=01", pos=(100,300), size=(100,100)),
                BoundButton(self, label="count=11", pos=(100,500), size=(100,100)),
            ],
            [ #count = 1
                BoundButton(self, label="count=12", pos=(250,500), size=(100,100)),
                BoundButton(self, label="count=22", pos=(250,500), size=(100,100)),
            ],
            [ #count = 2
                 wx.TextCtrl(self,-1,"{0:08d}".format(1),  pos=(100, 50), size=(50, 50)),
                 BoundButton(self, label="count23", pos=(350,600), size=(100,100)),
                 BoundButton(self, label="count21", pos=(350,600), size=(100,100)),
            ],
        ]
        self.updateFromCount()

    def OnB1(self,event):
        self.count = 0
        self.updateFromCount()
    def OnB2(self,event):
        self.count = 1
        self.updateFromCount()
    def OnB3(self,event):
        self.count = 2
        self.updateFromCount()
    def updateFromCount(self):
        hide_all(self.counts)
        show_all(self.counts[self.count])

a = wx.App(redirect=0)
f = mainFrame()
f.Show()
a.MainLoop()

但是在这个数组中,我没有发现如何

self.counts = [
                    [ #count =0
                        image=wx.Image(u"aaa.jpg",wx.BITMAP_TYPE_ANY),
                        self.bitmap_1 = wx.StaticBitmap(self, wx.ID_ANY, wx.BitmapFromImage(image.Rescale(350, 350)), (490,420)),
                        self.bitmap_1.Bind(wx.EVT_LEFT_DOWN, self.onClick,
                    ],
                    [ #count = 1
                        button_cancel = wx.Button(self, label="취소", pos=(490,550), size=(300,150)),
                        button_cancel.SetFont(wx.Font(40, wx.ROMAN, wx.SLANT, wx.BOLD, 0, "")),
                    ],
                ]

【问题讨论】:

    标签: python wxpython refresh reload


    【解决方案1】:

    您只是更改变量count__init__ 被调用一次。没有新的文本被追加改变或任何东西,只有变量计数被改变。在您的按钮侦听器中,您需要添加代码来更改文本。

    OnB* 函数中:

    def OnB1(self, event):
        self.count = 0
        self.text.SetLabel("%06d"%self.count)
    

    【讨论】:

    • 或者更简单的self.text.SetLabel("%06d"%self.count)
    • @JoranBeasley:是的,也许我不应该回答这些问题,因为我缺乏使用 wxWidgets 的经验。哈哈。谢谢!
    • @JoranBeasley:@Bair:非常感谢。我明白为什么它不起作用。但我找不到正确的答案。如何创建按钮 1、按钮 2、按钮 3、按钮 4?按照您的 OnB* 代码,它只能更改文本。我想做按钮和不相关的 text.SetLabel(OnB1 -> text.AppendText('1235535'))。
    • 什么?我不明白你在问什么
    • @Joran Beasley :我的意思是我怎样才能使它正确或工作。不可能???你能教我一些可以用几个按钮改变的代码吗?
    【解决方案2】:

    也许你正在寻找类似的东西

    import wx
    
    def BoundButton(*args,**kwargs):
        callback = kwargs.pop("callback",lambda e:None)
        btn = wx.Button(*args,**kwargs)
        btn.Bind(wx.EVT_BUTTON,callback)
        return btn
    
    def hide_all(itm):
        if isinstance(itm,(list,tuple)):
            for i in itm:
                hide_all(i)
        else:
            itm.Hide()
    
    def show_all(itm):
        if isinstance(itm,(list,tuple)):
            for i in itm:
                show_all(i)
        else:
            itm.Show()
    
    class mainFrame(wx.Frame):
        def __init__(self):
    
            wx.Frame.__init__(self, None, -1, "refresh&update", size=(900,600))
            self.count = 0
            self.counts = [
                [ #count =0
                    wx.TextCtrl(self,-1,'{0:08d}'.format(0),  pos=(0, 0), size=(100, 100)),
                    wx.TextCtrl(self,-1,'{0:08d}'.format(0),  pos=(100, 100), size=(100, 100)),
                    wx.TextCtrl(self,-1,'{0:08d}'.format(0),  pos=(170, 250), size=(100, 100)),
                    BoundButton(self, label="count0", pos=(150,450), size=(200,100),callback=self.OnB1),
                    BoundButton(self, label="count1", pos=(400,450), size=(200,100),callback=self.OnB1),
                    BoundButton(self, label="count2", pos=(650,450), size=(200,100),callback=self.OnB1),
                ],
                [ #count = 1
                     wx.TextCtrl(self,-1,"{0:08d}".format(1),  pos=(100, 50), size=(100, 100)),
                     BoundButton(self, label="1111111", pos=(450,100), size=(100,100),callback=self.OnB2),
                     BoundButton(self, label="1111111", pos=(250,100), size=(100,100),callback=self.OnB2),
                ],
    
            ]
            self.updateFromCount()
        def OnB1(self,e):
            self.Toggle(1)
        def OnB2(self,e):
            self.Toggle(0)
        def Toggle(self,i):
            self.count = i
            self.updateFromCount()
        def updateFromCount(self):
            hide_all(self.counts)
            for i in range(self.count+1):
                show_all(self.counts[i])
    
    a = wx.App(redirect=0)
    f = mainFrame()
    f.Show()
    a.MainLoop()
    

    【讨论】:

    • 非常感谢^^。你的回答对我很有帮助。我可以再问一件事吗?我试图应用我的代码。但我在 self.counts[] 中有一些错误(我的问题代码底部有错误代码)。
    猜你喜欢
    • 1970-01-01
    • 2016-10-18
    • 2011-10-03
    • 1970-01-01
    • 1970-01-01
    • 2019-05-23
    • 2012-05-09
    • 1970-01-01
    • 2021-12-10
    相关资源
    最近更新 更多