我正在使用wxPython在Python中编写GUI应用程序,我想在静态控件(wx.StaticBitmap)中显示图像.
我可以用来wx.ImageFromStream从文件加载图像,这可以正常工作:
static_bitmap = wx.StaticBitmap(parent, wx.ID_ANY)
f = open("test.jpg", "rb")
image = wx.ImageFromStream(f)
bitmap = wx.BitmapFromImage(image)
static_bitmap.SetBitmap(bitmap)
Run Code Online (Sandbox Code Playgroud)
但是,我真正希望能够做的是从内存中的数据创建图像.所以,如果我写
f = open("test.jpg", "rb")
data = f.read()
Run Code Online (Sandbox Code Playgroud)
我该如何创建一个wx.Image对象data?
谢谢你的帮助!
在下面的代码(灵感来自这个片段),我用一个单一的事件处理程序buttonClick来改变窗口的标题.目前,我需要评估事件的Id是否与按钮的Id相对应.如果我决定添加50个按钮而不是2个,则此方法可能会变得很麻烦.有一个更好的方法吗?
import wx
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, 'wxBitmapButton',
pos=(300, 150), size=(300, 350))
self.panel1 = wx.Panel(self, -1)
self.button1 = wx.Button(self.panel1, id=-1,
pos=(10, 20), size = (20,20))
self.button1.Bind(wx.EVT_BUTTON, self.buttonClick)
self.button2 = wx.Button(self.panel1, id=-1,
pos=(40, 20), size = (20,20))
self.button2.Bind(wx.EVT_BUTTON, self.buttonClick)
self.Show(True)
def buttonClick(self,event):
if event.Id == self.button1.Id:
self.SetTitle("Button 1 clicked")
elif event.Id == self.button2.Id:
self.SetTitle("Button 2 clicked")
application = wx.PySimpleApp()
window = MyFrame()
application.MainLoop()
Run Code Online (Sandbox Code Playgroud) 我开始学习Python和wxPython,作为我正在做的应用程序的一部分,我需要在我的应用程序的左侧窗格上有一个简单的浏览器.我想知道我该怎么办?或者至少指出正确的方向,这将有助于我更多地了解如何做一个.提前致谢!
编辑:一个侧面的问题,我需要学习多少wxPython?我应该使用像wxGlade这样的工具吗?
我有一个包含单个项目的WxPython框架,例如:
class Panel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.text = wx.StaticText(self, label='Panel 1')
Run Code Online (Sandbox Code Playgroud)
我有一个框架包含几个面板,包括这个面板,尺寸由sizer统治.我想StaticText扩大这一点.使用BoxSizer仅包含文本和设置wx.EXPAND标志就可以了,但是对于一个项目使用sizer似乎很愚蠢.
更简单的解决方案?
(我可以直接将其添加StaticText到父框架的sizer中,但对于我的设计,直接从框架开始更有意义.)
我刚刚意识到,创建BoxSizer一个项目时不能使用wx.VERTICAL:
class Panel1(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.BackgroundColour = 'red'
sizer = wx.BoxSizer(wx.VERTICAL)
self.Sizer = sizer
self.list = wx.ListBox(self, choices=[str(i) for i in xrange(100)])
sizer.Add(self.list, 0, wx.EXPAND)
sizer.Fit(self)
Run Code Online (Sandbox Code Playgroud)
当然可以使用一个项目,但如果我想稍后垂直添加项目并仍然使它们都展开(例如,当用户的窗口展开时)怎么办?
编辑:嗯,我刚发现必须使用这个比例才能让boxsizers以两种方式成长.(即,在1 BoxSizer.Add的呼叫中将0替换为0.)
我正在尝试下面的代码仍然它给我水平线..我错过了什么?
self.ln = wx.StaticLine(self,-1,wx.Point(620,0),wx.Size(687,7),wx.LI_VERTICAL)
self.ln.SetForegroundColour(wx.Colour(255,0,255))
嘿,我试图在用户单击图形上的某个点时在图像顶部绘制一个矩形。
要滚动浏览不同的图形,我使用了staticBitmap。不幸的是,几乎所有与DC的尝试都没有成功。PaintDC和BufferedDC有时会导致无限循环发生,而其他时候则将图形置于图像后面。ClientDC会显示我绘制的框,但是当我调整尺寸时它会消失。当我仅将图像保存到文件中但无法放置在staticBitmap中时,可以使用MemoryDC创建图形。
我花了大约一个星期的时间来解决这个问题,阅读了很多不同的教程和论坛来尝试找到同样的问题。我觉得没有其他人遇到这个问题。
每当调整窗口大小时,最重用的ClientDC必须重新绘制,从而导致闪烁。这是ClientDC的功能:
self.imageCtrl = wx.StaticBitmap(self.thePanel, wx.ID_ANY,
wx.EmptyBitmap(517,524))
def OnGoSelect(self,e):
print "GO"
img = wx.Image("./poster/"+self.picChoice,wx.BITMAP_TYPE_PNG)
self.imageCtrl.SetBitmap(wx.BitmapFromImage(img))
def DrawLine(self):
dc = wx.ClientDC(self.imageCtrl)
dc.SetPen(wx.Pen(wx.BLUE, 2))
dc.DrawLines(((223, 376), (223, 39), (240, 39), (240,376), (223,376)))
Run Code Online (Sandbox Code Playgroud)
当前的PaintDC不会进入无限循环,而是将图像放置在staticBitmap中,并且绘图以某种方式位于图像的后面。因此,当我调整大小时,ComboBoxes会擦除图像的一部分并调整窗口大小以覆盖图像,从而擦除该部分。当我调整窗口的大小时,图形仍然存在,但是图像被删除了。这是我所拥有的:
self.imageCtrl = wx.StaticBitmap(self.thePanel, wx.ID_ANY,
wx.EmptyBitmap(517,524))
def OnGoSelect(self,e):
print "GO"
img = wx.Image("./poster/"+self.picChoice,wx.BITMAP_TYPE_PNG)
self.imageCtrl.SetBitmap(wx.BitmapFromImage(img))
self.imageCtrl.Bind(wx.EVT_PAINT, self.OnPaint)
def OnPaint(self, e):
print "OnPaint Triggered"
dc = wx.PaintDC(self.imageCtrl)
dc.Clear()
dc.SetPen(wx.RED_PEN)
dc.DrawLines(((100, 200), (100, 100), (200, 100), (200,200), (100,200)))
Run Code Online (Sandbox Code Playgroud)
对于MemoryDC,我自己全部加载了EmptyBitmap,在其上绘制,然后尝试将其放入staticBitmap中。它给了我空白的灰色屏幕。如果我没有在EmptyBitmap上绘图,它会正常显示为黑色。即使在使用它时,我仍将其保存到文件中,该文件应以应有的方式出现,但仍使应用程序内部出现灰屏。这是MemoryDC代码:
self.imageCtrl = wx.StaticBitmap(self.thePanel, wx.ID_ANY,
wx.EmptyBitmap(517,524))
def Draw(self, e):
print "Draw"
img = …Run Code Online (Sandbox Code Playgroud) 我有一个我在wxPython中开发的应用程序.部分应用程序在网格中创建大量TextCtrls,以便为一个任意大的人员列表输入一周中每一天的四个字母代码.
我已经设法使它工作,但我必须做一些kludgy.具体来说,我还没有找到一个很好的方法来计算出一些文本的大小(以像素为单位),因此我必须猜测TextCtrls的大小以便恰好适合最大的(4个字母)代码.我担心的是,如果要更改文本的大小,或者将应用程序移植到MSW以外的其他内容,那么我的布局可能会中断.
我找到的解决方案是创建一个StaticText对象,用4个大字母填充它,得到它的大小,然后是.Destroy()它.这可行,但它看起来真的是一个笨重的解决方案.有没有人有更直接的方法来确定TextCtrl中任意文本字符串的大小?我查看了文档并且找不到任何内容,而且我没有看到任何正如我正在尝试做的那样的例子.
下面是一个小代码示例(显示测试方法):
# This creates and then immediately destroys a static text control
# in order to learn how big four characters are in the current font.
testst = wx.StaticText(self, id = wx.ID_ANY, label = "XXXX")
self.fourlettertextsize = testst.GetClientSize() + (4,4)
testst.Destroy()
Run Code Online (Sandbox Code Playgroud)
(+(4,4)部分是在控件中添加一点空间以容纳光标和TextCtrl的客户区域中的一个小边框;这是我不想直接做的另一件事.)
我有一个wx.grid表,当我将鼠标悬停在一个单元格上时,我想设置一个工具提示,我在下面尝试了Mike Driscoll的推荐,它可以工作,但是我不能用鼠标拖动选择多个单元格,它允许我只选择最多1个单元格,请帮助:
self.grid_area.GetGridWindow().Bind(wx.EVT_MOTION, self.onMouseOver)
def onMouseOver(self, event):
'''
Method to calculate where the mouse is pointing and
then set the tooltip dynamically.
'''
# Use CalcUnscrolledPosition() to get the mouse position
# within the
# entire grid including what's offscreen
x, y = self.grid_area.CalcUnscrolledPosition(event.GetX(),event.GetY())
coords = self.grid_area.XYToCell(x, y)
# you only need these if you need the value in the cell
row = coords[0]
col = coords[1]
if self.grid_area.GetCellValue(row, col):
if self.grid_area.GetCellValue(row, col) == "ABC":
event.GetEventObject().SetToolTipString("Code is abc")
elif self.grid_area.GetCellValue(row, …Run Code Online (Sandbox Code Playgroud) 我想将文件拖到窗口中并获取文件路径.我试过这样做:
class CSVDropper(wx.FileDropTarget):
def __init__(self, data):
wx.FileDropTarget.__init__(self)
self.data = data
def OnDropFiles(self, x, y, filenames):
self.data = filenames
print self.data
Run Code Online (Sandbox Code Playgroud)
然后在主窗口中:
# Drag & Drop
self.csv_path = None
self.drop_table = CSVDropper(self.csv_path)
self.SetDropTarget(self.drop_table)
Run Code Online (Sandbox Code Playgroud)
但这没有任何作用.我已经尝试过运行这个教程代码,但它也没有做任何事情.我该如何做到这一点?
如何将参数传递给animation()函数?,我试过但是工作.animation.FuncAnimation()的原型是
class matplotlib.animation.FuncAnimation(fig,func,frames = None,init_func = None,fargs = None,save_count = None,**kwargs)Bases:matplotlib.animation.TimedAnimation
我已粘贴下面的代码,我必须做哪些更改?
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def animate(i,argu):
print argu
graph_data = open('example.txt','r').read()
lines = graph_data.split('\n')
xs = []
ys = []
for line in lines:
if len(line) > 1:
x, y = line.split(',')
xs.append(x)
ys.append(y)
ax1.clear()
ax1.plot(xs, ys)
plt.grid()
ani = animation.FuncAnimation(fig,animate,fargs = 5,interval = 100)
plt.show()
Run Code Online (Sandbox Code Playgroud) wxpython ×10
python ×8
wxwidgets ×2
events ×1
filepath ×1
frame ×1
grid ×1
matplotlib ×1
mousehover ×1
size ×1
sizer ×1
tooltip ×1
wxtextctrl ×1