導航:首頁 > 編程語言 > pythonwx事件

pythonwx事件

發布時間:2025-06-10 16:57:21

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()

組件

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.

閱讀全文

與pythonwx事件相關的資料

熱點內容
androidsdk玩游戲 瀏覽:797
start文件夾刪除後果 瀏覽:543
java類main方法 瀏覽:84
程序員那麼可愛葉子的扮演者 瀏覽:498
冰櫃壓縮機做氣泵 瀏覽:45
快捷命令匯總表 瀏覽:830
wdk編譯驅動部署錯誤 瀏覽:438
美國伺服器有什麼優點 瀏覽:968
秘密演講pdf 瀏覽:728
雲伺服器區域代理商 瀏覽:71
linux0d 瀏覽:543
android郵件app 瀏覽:310
股票的app要測什麼東西 瀏覽:281
日語命令行禁止行 瀏覽:637
win10自帶c語言編譯器怎麼打開 瀏覽:132
cnc數控編程指令 瀏覽:103
mc伺服器無法連接至世界是怎麼回事 瀏覽:40
程序員那麼可愛z漫畫 瀏覽:392
置業顧問解壓是啥意思 瀏覽:135
java文檔怎麼寫 瀏覽:363