【问题标题】:Why the text in wxpython only display half if I change the text content如果我更改文本内容,为什么 wxpython 中的文本只显示一半
【发布时间】:2021-12-27 06:28:10
【问题描述】:

我现在正在使用 wxpython 并且想在运行期间更改文本内容,这是我的代码:

import wx
import time

def button_click(event):
    label2 = wx.StaticText(panel, pos=(25, 100))
    label2.SetSize(30, -1)
    label2.SetLabelText('test_b')
    label2.SetForegroundColour((255, 0, 0))
    label2.SetFont(font)
    time.sleep(1)
    label2.SetLabelText('chang_to_test_c')
    time.sleep(2)
    label2.SetLabelText('finally_is_test_d')

app = wx.App()
window = wx.Frame(None, title='test', size=(720, 480), pos=(200, 50))
panel = wx.Panel(window, size=(720, 480))
font = wx.Font(15, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD)
button1 = wx.Button(panel, label='click', pos=(25, 50), size=(100, 30))
button1.Bind(wx.EVT_BUTTON, button_click)
window.Show()
app.MainLoop()

当我运行程序并单击按钮时,它首先只显示'tes'和'chang_'这样

然后显示'finally_is_test_d',为什么wxpython在运行时只显示一半的文本,如果我想看到所有的文本怎么办。

【问题讨论】:

  • 您可能需要适当地设置wx.StaticText 的大小。或者缩小字体大小。
  • 我已经尝试了 并将字体大小设置为 10,而问题仍然存在,如果我删除 label2.SetLabelText('finally_is_test_d') 然后'change_to_test_c' 可以完全显示
  • 没有名为SetLabelText的函数。见this - 应该是SetLabel
  • @kiner_shah 在这种情况下,我认为您是正确的,但 SetLabelText 是有效的,并且与 SetLabel wxpython.org/Phoenix/docs/html/… 略有不同
  • Sizers 是棘手的野兽,但这是他们的魔法最有效的地方,在需要时自动调整小部件的大小。看看:wxpython.org/Phoenix/docs/html/sizers_overview.html

标签: python wxpython display


【解决方案1】:

睡觉是不对的:-)

您必须记住,wxPython 程序中的所有内容都在事件循环中发生 - app.MainLoop()。

事件循环调用您的 button_click 函数。它执行以下操作:

  • 更改文字
  • 睡觉
  • 更改文字
  • 睡觉
  • 再次更改文本

button_click 运行后,事件循环有另一个机会处理事件并因此重绘或重新布局。

我推荐两件事:

  • 查看 wx.Timer - 这将帮助您稍后或重复运行操作
  • 看看尺寸器。通常更喜欢使用大小调整器来布局小部件,而不是绝对大小。

【讨论】:

    【解决方案2】:

    正如我所说,您需要适当地设置文本框大小以避免文本剪切。此外,由于该标签是为按钮单击事件创建的,因此应在函数结束时将其销毁。

    我在构造函数本身中移动了设置初始标签和大小部分。

    同意@PetrBlahos,避免在事件监听器函数中休眠,sizer 很酷:-)

    import wx
    #import wx.lib.inspection
    import time
    
    def button_click(event):
        label2 = wx.StaticText(panel, label = "test_b", size = (720, 30), pos = (25, 100))
        #print(font)
        label2.SetForegroundColour((255, 0, 0))
        label2.SetFont(font)
        
        time.sleep(2)
        label2.SetLabel('chang_to_test_c')
        time.sleep(2)
        label2.SetLabel('finally_is_test_d')
        time.sleep(2)
        label2.Destroy()
    
    app = wx.App()
    window = wx.Frame(None, title='test', size=(720, 480), pos=(200, 50))
    panel = wx.Panel(window, size=(720, 480))
    font = wx.Font(15, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD)
    button1 = wx.Button(panel, label='click', pos=(25, 50), size=(100, 30))
    button1.Bind(wx.EVT_BUTTON, button_click)
    #wx.lib.inspection.InspectionTool().Show()
    window.Show()
    app.MainLoop()
    

    另外,根据@RolfofSaxony 的评论,您还可以使用派生自wx.ControlSetLabelText 方法。

    【讨论】:

      【解决方案3】:

      我已经解决了,原因是每个文本大小不同,而wxpython会使用前者的大小来显示后面的文本, 我已将代码更改如下:

      def button_click(event):
          label2 = wx.StaticText(panel, pos=(25, 100))
          label2.SetLabel('test_b           ')
          label2.SetForegroundColour((255, 0, 0))
          label2.SetFont(font)
          time.sleep(2)
          label2.SetLabel('chang_to_test_c  ')
          time.sleep(2)
          label2.SetLabel('finally_is_test_d')
          label2.Destroy()
      

      然后就可以了

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-15
        • 1970-01-01
        • 2020-06-29
        • 2019-01-25
        • 2023-03-28
        相关资源
        最近更新 更多