wxpython-如何在不移动其他所有内容的情况下隐藏和显示项目?
我有一个按钮可以从textctrl
中复制文本,我想在其旁边显示一条消息,以确认文本已复制到剪贴板上。因此,我正在使用隐藏并显示何时单击按钮的stalitctext
。
我已经将按钮和stalitctext
放在水平居中BoxSizer
中。事实是,当stalitctext
显示时,boxsizer
的孩子的中心都将其移动。这是我的代码:
def __init__(self, *args, **kw) -> None:
super(Window, self).__init__(*args, **kw)
# create a panel in the frame
pnl = wx.Panel(self)
# create some widgets in the panel
self.text_area1 = wx.TextCtrl(pnl, style=wx.TE_MULTILINE | wx.TE_NOHIDESEL)
self.text_area2 = wx.TextCtrl(pnl, style = wx.TE_MULTILINE | wx.TE_READONLY | wx.TE_NOHIDESEL | wx.TE_RICH2)
btn_correct = wx.Button(pnl, label="Correct >>>")
btn_correct.Bind(wx.EVT_BUTTON, self.on_press_correct)
self.btn_copy = wx.Button(pnl, label="Copy")
self.btn_copy.Bind(wx.EVT_BUTTON, self.on_press_copy)
self.label = wx.StaticText(pnl, label="Text copied")
self.label.Hide()
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.on_timer, self.timer)
# create a sizer to manage the layout of child widgets
sizer = wx.BoxSizer(wx.VERTICAL)
sub_sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(self.text_area1, 1, wx.ALL | wx.EXPAND, 5)
sizer.Add(btn_correct, 0, wx.ALL | wx.CENTER, 5)
sizer.Add(self.text_area2, 1, wx.ALL | wx.EXPAND, 5)
sub_sizer.Add(self.btn_copy, 0, wx.ALL | wx.CENTER, 5)
sub_sizer.Add(self.label, 0, wx.ALL | wx.CENTER, 5)
sizer.Add(self.sub_sizer, 0, wx.BOTTOM | wx.CENTER, 5)
pnl.SetSizer(sizer)
def on_press_copy(self, event):
data_obj = wx.TextDataObject()
data_obj.SetText(self.text_area2.GetValue())
if wx.TheClipboard.Open():
wx.TheClipboard.SetData(data_obj)
wx.TheClipboard.Close()
self.btn_copy.Disable()
self.label.Show()
self.label.GetParent().GetSizer().Layout()
self.timer.StartOnce(3000)
else:
wx.MessageBox("Can't open clipboard", "Error")
def on_timer(self, event):
self.btn_copy.Enable()
self.label.Show(show=False)
self.label.GetParent().GetSizer().Layout()
当stalitctext
显示时,如何在不移动按钮的情况下实现这一目标?
I have a button to copy text from a TextCtrl
and I want to display a message next to it to confirm that the text has been copied to the clipboard. So I'm using a StaticText
that hides and shows when the button is clicked.
I have put the button and the StaticText
in a horizontal centred BoxSizer
. The thing is when the StaticText
shows, as the BoxSizer
's children are centred it moves them. Here is my code:
def __init__(self, *args, **kw) -> None:
super(Window, self).__init__(*args, **kw)
# create a panel in the frame
pnl = wx.Panel(self)
# create some widgets in the panel
self.text_area1 = wx.TextCtrl(pnl, style=wx.TE_MULTILINE | wx.TE_NOHIDESEL)
self.text_area2 = wx.TextCtrl(pnl, style = wx.TE_MULTILINE | wx.TE_READONLY | wx.TE_NOHIDESEL | wx.TE_RICH2)
btn_correct = wx.Button(pnl, label="Correct >>>")
btn_correct.Bind(wx.EVT_BUTTON, self.on_press_correct)
self.btn_copy = wx.Button(pnl, label="Copy")
self.btn_copy.Bind(wx.EVT_BUTTON, self.on_press_copy)
self.label = wx.StaticText(pnl, label="Text copied")
self.label.Hide()
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.on_timer, self.timer)
# create a sizer to manage the layout of child widgets
sizer = wx.BoxSizer(wx.VERTICAL)
sub_sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(self.text_area1, 1, wx.ALL | wx.EXPAND, 5)
sizer.Add(btn_correct, 0, wx.ALL | wx.CENTER, 5)
sizer.Add(self.text_area2, 1, wx.ALL | wx.EXPAND, 5)
sub_sizer.Add(self.btn_copy, 0, wx.ALL | wx.CENTER, 5)
sub_sizer.Add(self.label, 0, wx.ALL | wx.CENTER, 5)
sizer.Add(self.sub_sizer, 0, wx.BOTTOM | wx.CENTER, 5)
pnl.SetSizer(sizer)
def on_press_copy(self, event):
data_obj = wx.TextDataObject()
data_obj.SetText(self.text_area2.GetValue())
if wx.TheClipboard.Open():
wx.TheClipboard.SetData(data_obj)
wx.TheClipboard.Close()
self.btn_copy.Disable()
self.label.Show()
self.label.GetParent().GetSizer().Layout()
self.timer.StartOnce(3000)
else:
wx.MessageBox("Can't open clipboard", "Error")
def on_timer(self, event):
self.btn_copy.Enable()
self.label.Show(show=False)
self.label.GetParent().GetSizer().Layout()
How can I achieve that without moving the button when the StaticText
shows?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我添加了一个虚拟元素(填充 - 大小> =标签文本的长度),并在按钮的左侧使用了它,并在右侧创建了一个sizer_sizer(包含填充元素的label_sizer)。这意味着显示文本时的按钮不会移动
I have added a dummy element (filler - size >= length of label text) and used that to the left of the button and created a sizer (label_sizer which contains the filler element) to the right. That means the button does not move when the text is shown
作为参考,有错误的部分代码片段无助于引起您的问题的关注。
显然,居中1项对2个项目永远不会起作用。总会有区别。
您需要找到另一种方法。
您可以输出消息框或更新状态行或使用不同的
sizer
,这可能会给您带来的近似值。例如
,在这里,我使用
GridBagsizer
,只需更新标签文本即可。它不是完美的,但是很近。
For reference, a partial code snippet, with errors, won't help get your question attention.
Obviously, centring 1 item versus 2 items, is never going to work. There will always be a difference.
You'll need to find another way.
You could output a messagebox or update the status line or use a different
sizer
, which probably gives you an approximation, of what you're after.e.g.
Here I use a
GridBagSizer
and simply update the label text.It isn't perfect but it's close.
我想这样的东西是状态栏是首选
I thought for stuff like that the status bar was first choice