代码: # show a jpeg (.jpg) image using wxPython, newer coding style # two different ways to load and display are given # tested with Python24 and wxPython25 vegaseat 24jul2005 import wx import cStringIO class Panel1(wx.Panel): """ class Panel1 creates a panel with an image on it, inherits wx.Panel """ def __init__(self, parent, id): # create the panel wx.Panel.__init__(self, parent, id) try: # pick a .jpg file you have in the working folder imageFile = 'Moo.jpg' data = open(imageFile, "rb").read() # convert to a data stream stream = cStringIO.StringIO(data) # convert to a bitmap bmp = wx.BitmapFromImage( wx.ImageFromStream( stream )) # show the bitmap, (5, 5) are upper left corner coordinates wx.StaticBitmap(self, -1, bmp, (5, 5)) # alternate (simpler) way to load and display a jpg image from a file # actually you can load .jpg .png .bmp or .gif files jpg1 = wx.Image(imageFile, wx.BITMAP_TYPE_ANY).ConvertToBitmap() # bitmap upper left corner is in the position tuple (x, y) = (5, 5) wx.StaticBitmap(self, -1, jpg1, (10 + jpg1.GetWidth(), 5), (jpg1.GetWidth(), jpg1.GetHeight())) except IOError: print "Image file %s not found" % imageFile raise SystemExit app = wx.PySimpleApp() # create a window/frame, no parent, -1 is default ID # increase the size of the frame for larger images frame1 = wx.Frame(None, -1, "An image on a panel", size = (400, 300)) # call the derived class Panel1(frame1,-1) frame1.Show(1) app.MainLoop()
我们在wxPyhton中想要设置背景图片,
或许你可能想直接在画布上绘制一张图片来实现其功能(wx.StaticBitmap),但是StaticBitmap widget 并不是一个容器,你的其他空间就会被覆盖掉。
现在我们可以利用 wx.EVT_ERASE_BACKGROUND巧妙地实现其功能,而且此方法不会覆盖你的其他的控件
见代码:
import wx
class Frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None,-1,"My Frame",size=(400,300),
style = wx.DEFAULT_FRAME_STYLE)
self.panel = wx.Panel(self)
self.panel.Bind(wx.EVT_ERASE_BACKGROUND,self.OnEraseBack)
def OnEraseBack(self,event):
dc = event.GetDC()
if not dc:
dc = wx.ClientDC(self)
rect = self.GetUpdateRegion().GetBox()
dc.SetClippingRect(rect)
dc.Clear()
bmp = wx.Bitmap("background.jpg")
dc.DrawBitmap(bmp, 0, 0)
if __name__ == '__main__':
app = wx.App()
frame = Frame()
frame.Show()
app.MainLoop()
代码:
# show a jpeg (.jpg) image using wxPython, newer coding style
# two different ways to load and display are given
# tested with Python24 and wxPython25 vegaseat 24jul2005
import wx
import cStringIO
class Panel1(wx.Panel):
""" class Panel1 creates a panel with an image on it, inherits wx.Panel """
def __init__(self, parent, id):
# create the panel
wx.Panel.__init__(self, parent, id)
try:
# pick a .jpg file you have in the working folder
imageFile = 'Moo.jpg'
data = open(imageFile, "rb").read()
# convert to a data stream
stream = cStringIO.StringIO(data)
# convert to a bitmap
bmp = wx.BitmapFromImage( wx.ImageFromStream( stream ))
# show the bitmap, (5, 5) are upper left corner coordinates
wx.StaticBitmap(self, -1, bmp, (5, 5))
# alternate (simpler) way to load and display a jpg image from a file
# actually you can load .jpg .png .bmp or .gif files
jpg1 = wx.Image(imageFile, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
# bitmap upper left corner is in the position tuple (x, y) = (5, 5)
wx.StaticBitmap(self, -1, jpg1, (10 + jpg1.GetWidth(), 5), (jpg1.GetWidth(), jpg1.GetHeight()))
except IOError:
print "Image file %s not found" % imageFile
raise SystemExit
app = wx.PySimpleApp()
# create a window/frame, no parent, -1 is default ID
# increase the size of the frame for larger images
frame1 = wx.Frame(None, -1, "An image on a panel", size = (400, 300))
# call the derived class
Panel1(frame1,-1)
frame1.Show(1)
app.MainLoop()
用wxformbuilder+一点代码来实现。
首先用wxformbuilder来做出界面,并生成代码。运行结果:
使其有背景图片效果:
#coding:utf-8
import wx
#导入wxformbuilder生成的代码
import BgDemo
class Main(wx.App):
def __init__(self, redirect=False, filename=None):
wx.App.__init__(self, redirect, filename)
dlg = BgDemo.MyFrame(None)
#这里设置哪个控件需要设置背景
dlg.m_panel1.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
dlg.Show()
def OnEraseBackground(self, evt):
"""
设置背景的方法
我们在wxPyhton中想要设置背景图片,
或许你可能想直接在画布上绘制一张图片来实现其功能(wx.StaticBitmap),但是StaticBitmap widget 并不是一个容器,你的其他空间就会被覆盖掉。
现在我们可以利用 wx.EVT_ERASE_BACKGROUND巧妙地实现其功能,而且此方法不会覆盖你的其他的控件
见代码:
import wx
class Frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None,-1,"My Frame",size=(400,300),
style = wx.DEFAULT_FRAME_STYLE)
self.panel = wx.Panel(self)
self.panel.Bind(wx.EVT_ERASE_BACKGROUND,self.OnEraseBack)
def OnEraseBack(self,event):
dc = event.GetDC()
if not dc:
dc = wx.ClientDC(self)
rect = self.GetUpdateRegion().GetBox()
dc.SetClippingRect(rect)
dc.Clear()
bmp = wx.Bitmap("background.jpg")
dc.DrawBitmap(bmp, 0, 0)
if __name__ == '__main__':
app = wx.App()
frame = Frame()
frame.Show()
app.MainLoop()
用wxformbuilder+一点代码来实现。
首先用wxformbuilder来做出界面,并生成代码。运行结果:
使其有背景图片效果:
#coding:utf-8
import wx
#导入wxformbuilder生成的代码
import BgDemo
class Main(wx.App):
def __init__(self, redirect=False, filename=None):
wx.App.__init__(self, redirect, filename)
dlg = BgDemo.MyFrame(None)
#这里设置哪个控件需要设置背景
dlg.m_panel1.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
dlg.Show()
def OnEraseBackground(self, evt):
"""
设置背景的方法
"""
dc = evt.GetDC()
if not dc:
dc = wx.ClientDC(self)
rect = self.GetUpdateRegion().GetBox()
dc.SetClippingRect(rect)
dc.Clear()
bmp = wx.Bitmap("Sunset.jpg")
dc.DrawBitmap(bmp, 0, 0)
if __name__ == "__main__":
app = Main()
app.MainLoop()
用wxformbuilder+一点代码来实现。
首先用wxformbuilder来做出界面,并生成代码。运行结果:
使其有背景图片效果:
#coding:utf-8
import wx
#导入wxformbuilder生成的代码
import BgDemo
class Main(wx.App):
def __init__(self, redirect=False, filename=None):
wx.App.__init__(self, redirect, filename)
dlg = BgDemo.MyFrame(None)
#这里设置哪个控件需要设置背景
dlg.m_panel1.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
dlg.Show()
def OnEraseBackground(self, evt):
"""
设置背景的方法
"""
dc = evt.GetDC()
if not dc:
dc = wx.ClientDC(self)
rect = self.GetUpdateRegion().GetBox()
dc.SetClippingRect(rect)
dc.Clear()
bmp = wx.Bitmap("Sunset.jpg")
dc.DrawBitmap(bmp, 0, 0)
if __name__ == "__main__":
app = Main()
app.MainLoop()
相关问题推荐
换行。比如,print hello\nworld效果就是helloworld\n就是一个换行符。\是转义的意思,'\n'是换行,'\t'是tab,'\\'是,\ 是在编写程序中句子太长百,人为换行后加上\但print出来是一整行。...
十种常见排序算法一般分为以下几种:(1)非线性时间比较类排序:a. 交换类排序(快速排序、冒泡排序)b. 插入类排序(简单插入排序、希尔排序)c. 选择类排序(简单选择排序、堆排序)d. 归并排序(二路归并排序、多路归并排序)(2)线性时间非比较类排序:...
前景很好,中国正在产业升级,工业机器人和人工智能方面都会是强烈的热点,而且正好是在3~5年以后的时间。难度,肯定高,要求你有创新的思维能力,高数中的微积分、数列等等必须得非常好,软件编程(基础的应用最广泛的语言:C/C++)必须得很好,微电子(数字电...
迭代器与生成器的区别:(1)生成器:生成器本质上就是一个函数,它记住了上一次返回时在函数体中的位置。对生成器函数的第二次(或第n次)调用,跳转到函数上一次挂起的位置。而且记录了程序执行的上下文。生成器不仅记住了它的数据状态,生成器还记住了程序...
python中title( )属于python中字符串函数,返回’标题化‘的字符串,就是单词的开头为大写,其余为小写
第一种解释:代码中的cnt是count的简称,一种电脑计算机内部的数学函数的名字,在Excel办公软件中计算参数列表中的数字项的个数;在数据库( sq| server或者access )中可以用来统计符合条件的数据条数。函数COUNT在计数时,将把数值型的数字计算进去;但是...
head是方法,所以需要取小括号,即dataset.head()显示的则是前5行。data[:, :-1]和data[:, -1]。另外,如果想通过位置取数据,请使用iloc,即dataset.iloc[:, :-1]和dataset.iloc[:, -1],前者表示的是取所有行,但不包括最后一列的数据,结果是个DataFrame。...
挺简单的,其实课程内容没有我们想象的那么难、像我之前同学,完全零基础,培训了半年,直接出来就工作了,人家还在北京大公司上班,一个月15k,实力老厉害了
Python针对众多的类型,提供了众多的内建函数来处理(内建是相对于导入import来说的,后面学习到包package时,将会介绍),这些内建函数功用在于其往往可对多种类型对象进行类似的操作,即多种类型对象的共有的操作;如果某种操作只对特殊的某一类对象可行,Pyt...
相当于 ... 这里不是注释
还有FIXME
python的两个库:xlrd和xlutils。 xlrd打开excel,但是打开的excel并不能直接写入数据,需要用xlutils主要是复制一份出来,实现后续的写入功能。
单行注释:Python中的单行注释一般是以#开头的,#右边的文字都会被当做解释说明的内容,不会被当做执行的程序。为了保证代码的可读性,一般会在#后面加一两个空格然后在编写解释内容。示例:# 单行注释print(hello world)注释可以放在代码上面也可以放在代...
主要是按行读取,然后就是写出判断逻辑来勘测行是否为注视行,空行,编码行其他的:import linecachefile=open('3_2.txt','r')linecount=len(file.readlines())linecache.getline('3_2.txt',linecount)这样做的过程中发现一个问题,...
或许是里面有没被注释的代码
自学的话要看个人情况,可以先在B站找一下视频看一下