- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Python 3.6 和 wxPython 4.1.0 gtk3 (phoenix) wxWidgets 3.1.4。我尝试绘制的每 4 行的 Bind 函数都有一个延迟,大约 12 行后程序崩溃。通常,我的代码使用网格来输入线条的点,但附加的代码会在单击 Draw 按钮后生成数据,效果完全相同。第 4 行的延迟约为 2.5 秒,通常绘制一条线需要 0.1 秒。我已经为 Bind 调用尝试了两个函数,一个使用事件,另一个调用对象。
如果有人知道解决方法,请告诉我。
import time
import string
import wx
from wx.lib.floatcanvas import NavCanvas, FloatCanvas
import wx.lib.colourdb
class InputForm(wx.Frame):
'''set up the form and draw axis'''
def __init__(self):
super(InputForm, self).__init__(None, wx.ID_ANY, title='Plot Lines', size=(1300, 830))
# set dictionary of points; key node letter, value tuple of point,
self.pts = {}
# create the form level sizer
Main_Sizer = wx.BoxSizer(wx.HORIZONTAL)
# add the sizer for the left side widgets
sizerL = wx.BoxSizer(wx.VERTICAL)
# add the grid and then set it ot he left panel
btnsizer = wx.BoxSizer(wx.HORIZONTAL)
drw = wx.Button(self, -1, "Draw\nLines")
btnsizer.Add(drw, 0, wx.ALL|wx.ALIGN_CENTER, 5)
# bind the button events to handlers
self.Bind(wx.EVT_BUTTON, self.OnDraw, drw)
sizerL.Add((10, 20))
sizerL.Add(btnsizer, 1, wx.ALIGN_CENTER)
# add the draw panel
rght = NavCanvas.NavCanvas(self,
ProjectionFun=None,
Debug=0,
BackgroundColor="LIGHT GREY",
)
self.Canvas = rght.Canvas
self.InitCanvas()
Main_Sizer.Add(sizerL, 0, wx.EXPAND)
Main_Sizer.Add((10, 10))
Main_Sizer.Add(rght, 1, wx.EXPAND)
self.SetSizer(Main_Sizer)
def InitCanvas(self):
# add the x & y axis
self.Canvas.AddLine([(0, 0), (0, 5)], LineWidth=2, LineColor='Yellow')
self.Canvas.AddLine([(0, 0), (5, 0)], LineWidth=2, LineColor='Green')
origin = self.Canvas.AddScaledTextBox('origin', (0, 0),
Color='blue',
Size=.5,
PadSize=0,
Width=None,
LineColor=None,
Family=wx.MODERN,
Position='tr',
Alignment='bottom',
InForeground=True)
# first Bind of node to EvtLeftDown
origin.Bind(FloatCanvas.EVT_FC_LEFT_DOWN,
lambda evt, selctEnd='Origin':
self.EvtLeftDown(evt, 'Origin'))
wx.CallAfter(self.Canvas.ZoomToBB)
def OnDraw(self, evt):
pts1 = (0, 0)
x = [i for i in range(5, 30, 2)]
y = x[::-1]
pts2 = [(x[i], y[i]) for i in range(0, len(x))]
alph = string.ascii_uppercase
LnLbls = [alph[i] for i in range(0, len(x))]
New_EndPt = True
n = 0
for pt in pts2:
points = []
points.append(pts1)
points.append(pt)
LnLbl = LnLbls[n]
New_EndPt = True
n += 1
self.DrawLine(points, LnLbl, New_EndPt)
def DrawLine(self, points, LnLbl, New_EndPt):
'''Draws the line object as specified in the VarifyData() function'''
# label the end point of the line in lower case
if New_EndPt is True:
new_end = self.Canvas.AddScaledTextBox(LnLbl.lower(), tuple(points[1]),
Color='black',
Size=.5,
PadSize=.2,
Width=None,
LineColor=None,
Family=wx.MODERN,
Position='cc',
Alignment='bottom',
InForeground=True)
new_end.Bind(FloatCanvas.EVT_FC_LEFT_DOWN,
lambda evt, selctEnd=LnLbl.lower():
self.EvtLeftDown(evt, selctEnd))
# define the new line
self.Canvas.AddLine(points, LineWidth=2, LineColor='red')
# add the new line to the list of lines
self.Canvas.AddPoint(tuple(points[1]), 'black', 8)
# locate the center of the new line for the label location
lncntr = ((int(points[0][0])+int(points[1][0]))//2,
(int(points[0][1])+int(points[1][1]))//2)
# place the new line lable
new_line = self.Canvas.AddScaledTextBox(LnLbl, lncntr,
Color='red',
Size=.5,
PadSize=None,
Width=None,
LineColor=None,
Family=wx.MODERN,
Position='tc',
Alignment='bottom',
InForeground=True)
new_line.Name = LnLbl
tic = time.perf_counter()
new_line.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.ObjLeftDown)
toc = time.perf_counter()
print(f'time to execute BIND function for DrawLine line 136 = {toc-tic:0.2f}')
wx.CallAfter(self.Canvas.ZoomToBB)
def ObjLeftDown(self, object):
lbl = object.Name
if lbl == 'Origin':
self.Node(lbl)
elif 65 <= ord(lbl) <= 90:
print('you have selected line ', lbl)
elif 97 <= ord(lbl) <= 122:
print('you have selected node ', lbl)
def EvtLeftDown(self, evt, lbl):
if lbl == 'Origin':
print('you have selected the origin')
elif 97 <= ord(lbl) <= 122:
print('you have selected node ', lbl)
# Run the program
if __name__ == "__main__":
app = wx.App(False)
frame = InputForm()
frame.Center()
frame.Show()
app.MainLoop()
最佳答案
对您的代码稍作调整,我无法复制 delays
你所指的。
无论我重新画线多少次,时间总是在大致相同的时间范围内。
绑定(bind)是针对不同的对象,它只是将一个事件绑定(bind)到一个对象,所以我怀疑这是问题所在。
import time
import string
import wx
from wx.lib.floatcanvas import NavCanvas, FloatCanvas
import wx.lib.colourdb
class InputForm(wx.Frame):
'''set up the form and draw axis'''
def __init__(self):
super(InputForm, self).__init__(None, wx.ID_ANY, title='Plot Lines', size=(1300, 830))
# set dictionary of points; key node letter, value tuple of point,
self.pts = {}
self.draw_repetitions = 0
# create the form level sizer
Main_Sizer = wx.BoxSizer(wx.HORIZONTAL)
# add the sizer for the left side widgets
sizerL = wx.BoxSizer(wx.VERTICAL)
# add the grid and then set it ot he left panel
btnsizer = wx.BoxSizer(wx.HORIZONTAL)
drw = wx.Button(self, -1, "Draw\nLines")
btnsizer.Add(drw, 0, wx.ALL|wx.ALIGN_CENTER, 5)
# bind the button events to handlers
self.Bind(wx.EVT_BUTTON, self.OnDraw, drw)
sizerL.Add((10, 20))
sizerL.Add(btnsizer, 1, wx.ALIGN_CENTER)
# add the draw panel
self.rght = NavCanvas.NavCanvas(self,
ProjectionFun=None,
Debug=0,
BackgroundColor="LIGHT GREY",
)
#self.Canvas = self.rght.Canvas
self.InitCanvas()
Main_Sizer.Add(sizerL, 0, wx.EXPAND)
Main_Sizer.Add((10, 10))
Main_Sizer.Add(self.rght, 1, wx.EXPAND)
self.SetSizer(Main_Sizer)
def InitCanvas(self):
# add the x & y axis
self.Canvas = self.rght.Canvas
self.Canvas.ClearAll()
self.Canvas.AddLine([(0, 0), (0, 5)], LineWidth=2, LineColor='Yellow')
self.Canvas.AddLine([(0, 0), (5, 0)], LineWidth=2, LineColor='Green')
origin = self.Canvas.AddScaledTextBox('origin', (0, 0),
Color='blue',
Size=.5,
PadSize=0,
Width=None,
LineColor=None,
Family=wx.MODERN,
Position='tr',
Alignment='bottom',
InForeground=True)
# first Bind of node to EvtLeftDown
origin.Bind(FloatCanvas.EVT_FC_LEFT_DOWN,
lambda evt, selctEnd='Origin':
self.EvtLeftDown(evt, 'Origin'))
wx.CallAfter(self.Canvas.ZoomToBB)
def OnDraw(self, evt):
self.InitCanvas()
pts1 = (0, 0)
x = [i for i in range(5, 30, 2)]
y = x[::-1]
pts2 = [(x[i], y[i]) for i in range(0, len(x))]
alph = string.ascii_uppercase
LnLbls = [alph[i] for i in range(0, len(x))]
New_EndPt = True
n = 0
for pt in pts2:
points = []
points.append(pts1)
points.append(pt)
LnLbl = LnLbls[n]
New_EndPt = True
n += 1
self.DrawLine(points, LnLbl, New_EndPt)
def DrawLine(self, points, LnLbl, New_EndPt):
'''Draws the line object as specified in the VarifyData() function'''
self.draw_repetitions += 1
# label the end point of the line in lower case
if New_EndPt is True:
new_end = self.Canvas.AddScaledTextBox(LnLbl.lower(), tuple(points[1]),
Color='black',
Size=.5,
PadSize=.2,
Width=None,
LineColor=None,
Family=wx.MODERN,
Position='cc',
Alignment='bottom',
InForeground=True)
new_end.Bind(FloatCanvas.EVT_FC_LEFT_DOWN,
lambda evt, selctEnd=LnLbl.lower():
self.EvtLeftDown(evt, selctEnd))
# define the new line
self.Canvas.AddLine(points, LineWidth=2, LineColor='red')
# add the new line to the list of lines
self.Canvas.AddPoint(tuple(points[1]), 'black', 8)
# locate the center of the new line for the label location
lncntr = ((int(points[0][0])+int(points[1][0]))//2,
(int(points[0][1])+int(points[1][1]))//2)
# place the new line lable
new_line = self.Canvas.AddScaledTextBox(LnLbl, lncntr,
Color='red',
Size=.5,
PadSize=None,
Width=None,
LineColor=None,
Family=wx.MODERN,
Position='tc',
Alignment='bottom',
InForeground=True)
new_line.Name = LnLbl
tic = time.perf_counter()
new_line.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.ObjLeftDown)
toc = time.perf_counter()
print(f'time to execute BIND function for DrawLine line ',LnLbl, toc-tic)
print(f'Draw repetitions ',self.draw_repetitions)
# wx.CallAfter(self.Canvas.ZoomToBB)
self.Canvas.ZoomToBB()
def ObjLeftDown(self, object):
lbl = object.Name
if lbl == 'Origin':
self.Node(lbl)
print(dir(self.Node))
elif 65 <= ord(lbl) <= 90:
print('you have selected line ', lbl)
elif 97 <= ord(lbl) <= 122:
print('you have selected node ', lbl)
def EvtLeftDown(self, evt, lbl):
if lbl == 'Origin':
print('you have selected the origin')
elif 97 <= ord(lbl) <= 122:
print('you have selected node ', lbl)
# try:
# evt.Skip()
# except:
# pass
# Run the program
if __name__ == "__main__":
app = wx.App(False)
frame = InputForm()
frame.Center()
frame.Show()
app.MainLoop()
关于performance - wxPython FloatCanvas 事件绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63320667/
您好,我想知道是否有办法检查是否按住了某个键。 这是一个情况示例 self.button2.Bind(wx.EVT_LEFT_DOWN, self.clickedbutton) def clicked
我正在使用 wxPython 设计一个表单。请注意图片中的搜索面板:“搜索依据:”标签、嵌套在它上面的组合框和搜索控件——它们的文本都与顶部对齐。如何将它们全部对齐(垂直)在中间? 这是搜索面板代码:
我对以下代码有一个我不明白的问题。 为什么是panel1不扩大? 谢谢。 import wx class MyFrame(wx.Frame): def __init__(self, paren
我刚刚开始使用 wxPython。我有以下代码: import wx class SASFrame(wx.Frame): def __init__(self,parent,id,title):
是否可以以列表的形式获取wx.ListCtrl列中的所有值(项目)? 我在文档中看到您可以获取指定的项目,但不能获取整个列,GetValue() 也不起作用,有什么帮助吗? 最佳答案 这是一种方法:
我需要通过单击按钮在 wxpython 中创建一个额外的新窗口(与主窗口在物理上是分开的)。我想在不关闭最后一个窗口的情况下执行此操作。 这是我到目前为止所拥有的: class Prototype(w
我正在创建一个文件对话框,允许用户在我的应用程序中编辑文件后保存文件。我想在对话框中添加一个复选框,以便用户可以选择保存文件的格式。我想我需要创建一些继承自 FileDialog 的新类,并将一个复选
我正在创建一个文件对话框,允许用户在我的应用程序中编辑文件后保存文件。我想在对话框中添加一个复选框,以便用户可以选择保存文件的格式。我想我需要创建一些继承自 FileDialog 的新类,并将一个复选
我已经编写了我的第一个 wxpython 应用程序并且它运行良好,但是我在正确调整主窗口的大小方面遇到了困难。该面板包含一个 wx.BoxSizer,后者又包含一个 wx.FlexGridSizer,
在 wxpython 中使用 SetTransparent(val) 可以使整个框架透明。但是我可以让其中的单个面板透明吗? 我试过使用 panelobj.SetTransparent(val) 但没
我读了这个Style Guide for wxPython code其中说: 3. Don't use IDs. There is very rarely a good reason to use t
我的顶级窗口需要知道自定义控件的内部状态何时发生变化,以便它可以更新 GUI 的各个其他部分。如何在控件中生成自定义事件,以便它可以传播并由顶级窗口处理? 最佳答案 我知道这是一个老问题,但是在 wx
如果这是正确的词,我正在寻找一种方法来获取小部件,例如面板设备上下文。我想获取面板的位图及其所有子小部件,以便我可以在油漆 DC 中操纵它以淡入淡出面板的动画。这是可能的,如果是这样,代码是什么?我希
我在我的 RHEL6 系统上安装了 Anaconda Python。为了运行 gnuradio,我需要 wxPython。出于这个原因,我做到了 conda install wxpython 问题是当
我想要一个只接受数字的文本控件。 (只是整数值,例如 45 或 366) 最好的方法是什么? 最佳答案 我必须做类似的事情,检查字母数字代码。 EVT_CHAR 上的提示是正确的: class Tes
用于 wxPython 的最佳拖放式 GUI 生成器?我试过 wxGlade 但它不是拖放,发现它很难理解,你必须使用 sizer。我更喜欢像 VisualTinker 那样完全拖放的应用程序,但对于
问题:我在 Ubuntu 12.04 上安装 wxPython 时遇到问题。我想找到一种简单直接的方法来做到这一点。 到目前为止我做了什么:到目前为止我能找到的最相关的说明可以在这里找到:http:/
我正在尝试在 wxPython 2.8(使用 Python 2.7)中编写一个 GUI,其中涉及在 sizer 中布置几个 wx.StaticText。我想要的是一个 sizer,如果它检测到当前行的
我有一个只有一个 child 的 wx.Frame。在设置 child 的 wx.Sizer 时,我可以使用 wx.SHAPED 标志,它保持 child 的宽高比锁定。但是,Frame 在纵横比方面
我已经尝试了很多次来找到这个路径但无济于事(链接:How do I import modules in pycharm?)我认为这是因为我使用的是较新版本的 PyCharm,但这是我的项目解释器: 我
我是一名优秀的程序员,十分优秀!