1. python 请教一下如何用wincon监听windouws的WM_QUERYENDSESSION关机事件,触发一个事件。
1. 安装python
2. 打开命令行输入python并回车,如果出现下图这样的,就说明安装成功
3. 写一个python并保存
print "hello world" # python2的写法
# 或者
print("hello world") # python3的写法
4. 再次打开命令行,输入 python + 要运行的python脚本的完整路径
2. python 编写gui界面有哪些
安装wxPython
下面是wxPython特别基础的使用方法,将以一个小程序为例来说明,它的功能是在一个文本框中输入文件名,点击open按钮,会在另一个文本框中显示其内容,可以进行修改,点击save可以保存修改。
wxPython的使用简介
创建应用程序对象:wx.App()
app=wx.App()
app.MainLoop()
窗口/框架:wx.Frame()
win=wx.Frame(None,title='example')
win.Show()
组件
按钮:wx.Button()
关键字label设置标签:
btn=wx.Button(win,label='open')
绑定事件:
btn.Bind(wx.EVT_BUTTON,load)
文本控件:wx.TextCtrl()
关键字style有wx.TE_MULTILINE(垂直滚动条),wx.HSXROLL(水平滚动条):
filename=wx.TextCtrl(win,style=wx.TE_MULTILINE|wx.HSXROLL)
同时我们可以在读取或者写入其内容:
filename.GetValue() filename.SetValue('string')
布局:使用尺寸器(sizer)
wx.BoxSizer的原理是创建一个箱子一样的东西,只能横着或者竖着放组件,一个一个的加进去,除了基本大小外其余空间按比例分配。加完一行行的箱子后,创建一个竖着的箱子,将这些横着的箱子(也可以是其他组件)加进去。
*背景组件:wx.Panel()
以窗口为参数构建,所有的组件依附其上,在设置完尺寸器后,使用bkg.SetSizer(box)
尺寸器的构建函数:wx.BoxSizer()
有一个决定它是垂直还是水平的参数(wx.HORIZONTAL水平,wx.VERTICAL垂直),默认为水平。
box=wx.BoxSizer(wx.VERTICAL)
Add方法
proportion参数决定这个组件在剩余空间中的分配比例,flag参数有wx.EXPAND(会确保组件会扩展到分配的空间里),wx.LEFT,wx.RIGHT,wx.TOP,wx.BOTTOM,wx.ALL(这五个决定边框参数会应用到那个边),border参数会确定边框的大小。
完整版的源代码如下:
3. Python中用wx.MessageDialog生成对话框,wx.ICON_QUESTION不能显示问号图标。
import wx
class MyFrame(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, u'测试面板Panel', size = (600, 300))
#创建面板
panel = wx.Panel(self)
#在Panel上添加Button
button = wx.Button(panel, label = u'关闭', pos = (150, 60), size = (100, 60))
#绑定单击事件
self.Bind(wx.EVT_BUTTON, self.OnCloseMe, button)
def OnCloseMe(self, event):
dlg = wx.MessageDialog(None, u"消息对话框测试", u"标题信息", wx.YES_NO | wx.ICON_QUESTION)
if dlg.ShowModal() == wx.ID_YES:
self.Close(True)
dlg.Destroy()
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = MyFrame(parent = None, id = -1)
frame.Show()
app.MainLoop()
4. python用wxpython的combobox怎么能让在它里面输入字符的时候,下拉列表自动给出含有该字符的可选列表
要实现这种自动补全的功能,可以使用wxPython中的wx.ComboBox控件,并自定义一个类来实现自动补全。以下是一个简单的代码示例:
importwx
classAutoCompleteComboBox(wx.ComboBox):
def__init__(self,*args,**kwargs):
super(AutoCompleteComboBox,self).__init__(*args,**kwargs)
self.choices=[]
self.Bind(wx.EVT_TEXT,self.OnText)
self.Bind(wx.EVT_COMBOBOX,self.OnSelect)
defSetChoices(self,choices):
self.choices=choices
defOnText(self,event):
input_text=event.GetString()
ifnotinput_text:
self.SetItems(self.choices)
return
filtered_choices=[choiceforchoiceinself.choicesifinput_text.lower()inchoice.lower()]
self.SetItems(filtered_choices)
self.Popup()
defOnSelect(self,event):
pass
classMyFrame(wx.Frame):
def__init__(self,parent,title):
super(MyFrame,self).__init__(parent,title=title,size=(400,300))
panel=wx.Panel(self)
self.Bind(wx.EVT_CLOSE,self.OnClose)
choices=["Apple","Banana","Cherry","Date","Fig","Grape","Kiwi","Lemon","Mango","Orange"]
self.combo_box=AutoCompleteComboBox(panel,pos=(50,50),size=(200,-1),choices=choices,style=wx.CB_DROPDOWN)
self.combo_box.SetChoices(choices)
defOnClose(self,event):
self.Destroy()
if__name__=="__main__":
app=wx.App(False)
frame=MyFrame(None,"AutoCompleteComboBoxExample")
frame.Show()
app.MainLoop()
在这个例子中,我们首先创建了一个AutoCompleteComboBox类,它继承自wx.ComboBox。我们为这个类添加了SetChoices方法,用于设置所有可能的选项。然后,我们通过监听wx.EVT_TEXT事件来监听输入框中文本的变化。当文本发生变化时,我们根据输入的文本筛选下拉列表中的选项,并显示筛选后的结果。
请注意,这个示例使用的数据是一个固定的字符串行表。如果您需要从数据库中获取数据,只需将choices替换为从数据库查询得到的结果。
…………
回复:
经过仔细检查,我发现在 OnText 方法中更新 ComboBox 项目时确实存在问题。当我们使用 SetItems 方法设置项目时,文本框内容会被清除,从而触发另一个 wx.EVT_TEXT 事件。因此,我们需要稍微修改 OnText 方法以避免无限递归。
以下是修复后的代码示例:
import wx
class AutoCompleteComboBox(wx.ComboBox):
def __init__(self, *args, **kwargs):
super(AutoCompleteComboBox, self).__init__(*args, **kwargs)
self.choices = []
self.Bind(wx.EVT_TEXT, self.OnText)
self.Bind(wx.EVT_COMBOBOX, self.OnSelect)
def SetChoices(self, choices):
self.choices = choices
def OnText(self, event):
input_text = event.GetString()
if not input_text:
self.SetItems(self.choices)
return
filtered_choices = [choice for choice in self.choices if input_text.lower() in choice.lower()]
# Avoid recursive call to OnText when setting items.
self.Unbind(wx.EVT_TEXT)
self.SetItems(filtered_choices)
self.Bind(wx.EVT_TEXT, self.OnText)
self.SetValue(input_text) # Reset the text value after updating items
self.SetInsertionPointEnd() # Move the cursor to the end of the text
self.Popup()
def OnSelect(self, event):
pass
# ... (the rest of the code remains unchanged)
这里的关键更改是在设置新项目之前取消绑定 wx.EVT_TEXT 事件,然后在设置项目后重新绑定事件。这样,在我们更新项目时就不会触发 OnText 方法。我们还使用 SetValue 方法重新设置文本值,并将光标移至文本末尾。
经过这些更改,代码应该可以正常运行,不会导致无限递归和不断增长的项目列表。
5. 通过自制的Python Wx问题,怎么解决
在github上下载了,安装了之后在idle上运行,好像是说Python不能上网。新手求助。现在问题已经解决,是ssl 证书的问题,不能用最新的
复制内容到剪贴板
代码:
sudo pip uninstall -y certifi
sudo pip install certifi==2015.04.28
>>> bot=Bot()
Traceback (most recent call last):
File "D:\Python34\lib\site-packages\urllib3\connectionpool.py", line 600, in urlopen
chunked=chunked)
File "D:\Python34\lib\site-packages\urllib3\御握connectionpool.py", line 345, in _make_request
self._validate_conn(conn)
File "D:\Python34\lib\site-packages\urllib3\connectionpool.py", line 844, in _validate_conn
conn.connect()
File "D:\Python34\lib\site-packages\urllib3\connection.py", line 326, in connect
ssl_context=context)
File "D:\Python34\lib\site-packages\urllib3\util\ssl_.py", line 325, in ssl_wrap_socket
return context.wrap_socket(sock, server_hostname=server_hostname)
File "D:\Python34\lib\ssl.py", line 365, in wrap_socket
_context=self)
File "D:\Python34\lib\ssl.py", line 583, in __init__
self.do_handshake()
File "D:\Python34\lib\ssl.py", line 810, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:600)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\Python34\lib\site-packages\requests\adapters.py", line 440, in send
timeout=timeout
File "镇吵庆D:\Python34\lib\site-packages\urllib3\connectionpool.py", line 630, in urlopen
raise SSLError(e)
urllib3.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:600)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\Python34\lib\site-packages\碰腊itchat\utils.py", line 124, in test_connect
r = requests.get(config.BASE_URL)
File "D:\Python34\lib\site-packages\requests\api.py", line 72, in get
return request('get', url, params=params, **kwargs)
File "D:\Python34\lib\site-packages\requests\api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "D:\Python34\lib\site-packages\requests\sessions.py", line 502, in request
resp = self.send(prep, **send_kwargs)
File "D:\Python34\lib\site-packages\requests\sessions.py", line 612, in send
r = adapter.send(request, **kwargs)
File "D:\Python34\lib\site-packages\requests\adapters.py", line 514, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:600)
You can't get access to internet or wechat domain, so exit.