【发布时间】:2019-06-28 07:39:30
【问题描述】:
我正在尝试使用 for 循环显示图像文件夹中的所有图像,并且所有图像都正确显示。但是我想在运行时添加另一个图像,所以我在这里使用线程threading.Timer(2.0, self.basicgui).start() 每当我将新图像添加到文件夹时,我可以在图像列表中看到图像的名称,但无法更新。我想刷新面板以显示更新的图像我该如何解决这个问题。
import wx
import threading
from PIL import Image
import wx.lib.scrolledpanel
import os
class windowclass(wx.Frame):
def __init__(self, *args, **kwargs):
super(windowclass, self).__init__(*args, **kwargs)
global panel1
panel1 = wx.Panel(self,size=(1000,28), pos=(0,0), style=wx.SIMPLE_BORDER)
panel1.SetBackgroundColour('#FDDF99')
global panel2
panel2 = wx.lib.scrolledpanel.ScrolledPanel(panel1,-1, size=(350,710), pos=(1010,0), style=wx.SIMPLE_BORDER)
panel2.SetupScrolling()
panel2.SetBackgroundColour('#FFFFFF')
self.basicgui()
def basicgui(self):
padding = 0
bSizer = wx.BoxSizer( wx.VERTICAL )
self.jpgs = GetJpgList("./image")
allimage = len(self.jpgs)
print(allimage)
for i in range(0,allimage):
#print("i=",i)
bitmap = wx.Bitmap(self.jpgs[i])
image = wx.ImageFromBitmap(bitmap)
image = image.Scale(300, 200, wx.IMAGE_QUALITY_HIGH)
bitmap = wx.BitmapFromImage(image)
control = wx.StaticBitmap(panel2, -1, bitmap)
#print(control)
control.SetPosition((10, padding+10))
padding += 210
bSizer.Add( control, 0, wx.ALL, 5 )
panel2.SetSizer( bSizer )
panel2.Layout()
threading.Timer(2.0, self.basicgui).start()
self.Show()
return
def GetJpgList(dir):
jpgs = [f for f in os.listdir(dir) if f[-4:] == ".jpg"]
# print "JPGS are:", jpgs
return [os.path.join(dir, f) for f in jpgs]
def main():
app = wx.App()
windowclass(None)
app.MainLoop()
main()
【问题讨论】:
-
您可以改用
wx.Timer。 -
如何使用 wx.Timer 。
-
而不是
threading.Timer使用wx.Timer回调到basicgui,仅此站点上 wx.timer 就有 306 次点击。 wx.Timer 每 (n) 毫秒运行一次回调,直到它停止。 -
它有效,但我遇到了一个问题。滚动条未显示
-
在回调中再次调用 panel2 self.panel2 和 Issue
self.panel2.SetupScrolling()。
标签: python python-3.x wxpython