gpt4 book ai didi

python - 关闭 WxPython GUI 的转义键

转载 作者:太空狗 更新时间:2023-10-29 21:44:02 24 4
gpt4 key购买 nike

我正在开发一个相当简单的 wxpython GUI,并且希望能够使用转义键关闭窗口。现在我只有一个执行 sys.exit(0) 的关闭按钮,但我想要转义键来执行此操作。

有谁知道这样做的方法吗?

import win32clipboard
import wx
from time import sleep
import sys

class MainFrame(wx.Frame):
def __init__(self,title):

wx.Frame.__init__(self, None, title="-RRESI Rounder-", pos=(0,0), size=(210,160))
panel=Panel(self)

icon = wx.Icon('ruler_ico.ico', wx.BITMAP_TYPE_ICO)
self.SetIcon(icon)

class Panel(wx.Panel):

def __init__(self, parent):
wx.Panel.__init__(self, parent)

x1=10; x2=110
y1=40; dy=25; ddy=-3
boxlength=80

self.button =wx.Button(self, label="GO", pos=(100,y1+dy*3))
self.Bind(wx.EVT_BUTTON, self.OnClick,self.button)
self.button.SetDefault()

self.button2 =wx.Button(self, label="Close", pos=(100,y1+dy*4))
self.Bind(wx.EVT_BUTTON, self.OnClose, self.button2)
self.button.SetDefault()

self.Bind(wx.EVT_KEY_UP, self.OnKeyUP)

self.label1 = wx.StaticText(self, label="Input Number:", pos=(x1,y1+dy*1))
self.Input = wx.TextCtrl(self, value="1.001", pos=(x2,ddy+y1+dy*1), size=(boxlength,-1))

self.label0 = wx.StaticText(self, label="Round to closest: 1/", pos=(x1,y1+dy*0))
self.Denominator = wx.TextCtrl(self, value="64", pos=(x2,ddy+y1+dy*0), size=(boxlength,-1))

self.label2 = wx.StaticText(self, label="Output Number:", pos=(x1,y1+dy*2))
self.display = wx.TextCtrl(self, value="1.0", pos=(x2,ddy+y1+dy*2), size=(boxlength,-1))
self.display.SetBackgroundColour(wx.Colour(232, 232, 232))

self.label3 = wx.StaticText(self, label=" ", pos=(x2+7,y1+dy*2+20)) #Copied

self.label4 = wx.StaticText(self, label="Type value and hit Enter", pos=(x1-5,y1-dy*1.5))
self.label5 = wx.StaticText(self, label="Output is copied", pos=(x1-5,y1-dy*1))
font = wx.Font(8, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
self.label4.SetFont(font)
self.label5.SetFont(font)

def OnKeyUP(self, event):
keyCode = event.GetKeyCode()
if keyCode == wx.WXK_ESCAPE:
sys.exit(0)


def OnClose(self, event):
print "Closed"
sys.exit(0)

def OnClick(self,event):
print "You clicked the button!"

def openClipboard():
try:
win32clipboard.OpenClipboard()
except Exception, e:
print e
pass

def closeClipboard():
try:
win32clipboard.CloseClipboard()
except Exception, e:
print e
pass

def clearClipboard():
try:
openClipboard()
win32clipboard.EmptyClipboard()
closeClipboard()
except TypeError:
pass

def setText(txt):
openClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardText(txt)
closeClipboard()

Denominator = float(self.Denominator.GetValue())
Input=float(self.Input.GetValue())
Output=round(Input*Denominator,0)/Denominator
self.display.SetValue(str(Output))
setText(str(Output))
self.label3.SetLabel("Copied")
self.Update()#force redraw
sleep(.5)
wx.Timer
self.label3.SetLabel(" ")


if __name__=="__main__":
app = wx.App(redirect=False) # Error messages don't go to popup window
frame = MainFrame("RRESI Rounder")
frame.Show()
app.MainLoop()

最佳答案

这个排序是可行的......虽然有一些问题

[编辑]好的 EVT_CHAR_HOOK 比 EVT_KEY_UP 工作得更好

import wx

class Test(wx.Frame):

def __init__(self):
wx.Frame.__init__(self, None, -1, title='Event Test',
size=(200, 200))

panel = wx.Panel(self)

panel.SetFocus()

self.Bind(wx.EVT_CHAR_HOOK, self.OnKeyUP)

def OnKeyUP(self, event):
print "KEY UP!"
keyCode = event.GetKeyCode()
if keyCode == wx.WXK_ESCAPE:
self.Close()
event.Skip()


class App(wx.App):
"""Application class."""

def OnInit(self):
self.frame = Test()
self.frame.Show()
self.SetTopWindow(self.frame)
self.frame.SetFocus()
return True

if __name__ == '__main__':
app = App()
app.MainLoop()

关于python - 关闭 WxPython GUI 的转义键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12304732/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com