What is the difference between parent.Bind and widget.Bind in wxPython - python

import wx
class MyPanel(wx.Panel):
def __init__(self, parent):
super().__init__(parent)
btn = wx.Button(self, label="Press me")
btn.Bind(wx.EVT_BUTTON, self.on_button_press)
def on_button_press(self, event):
print("You pressed the button")
class MyFrame(wx.Frame):
def __init__(self):
super().__init__(parent=None, title="Hello wxPython")
panel = MyPanel(self)
self.Show()
if __name__ == "__main__":
app = wx.App(redirect=False)
frame = MyFrame()
app.MainLoop()
In the code above we used btn.Bind for binding wx.Button to wx.EVT_BUTTON. if instead, we use this way:
self.Bind(wx.EVT_BUTTON, self.on_button_press, btn)
The result will be the same as above. Now my question is the difference between self.Bind and btn.Bind.

Each widget has an Id.
Events when triggered, pass the Id of the triggering widget, in this case a button.
Binding an event to a function can be specific or generic i.e. a specific widget or any widget that fires that event type.
In short, in this case, the self.Bind binds any button event unless you specify a widget ID.
See: https://docs.wxpython.org/events_overview.html
Hopefully, the code below will help explain.
N.B. event.Skip() says don't stop at this event, see if there are more events to process.
import wx
class MyPanel(wx.Panel):
def __init__(self, parent):
super().__init__(parent)
btn1 = wx.Button(self, label="Press me 1", pos=(10,10))
btn2 = wx.Button(self, label="Press me 2", pos=(10,50))
Abtn = wx.Button(self, label="Press me", pos=(10,90))
# Bind btn1 to a specific callback routine
btn1.Bind(wx.EVT_BUTTON, self.on_button1_press)
# Bind btn2 to a specific callback routine specifying its Id
# Note the order of precedence in the callback routines
self.Bind(wx.EVT_BUTTON, self.on_button2_press, btn2)
# or identify the widget via its number
# self.Bind(wx.EVT_BUTTON, self.on_button2_press, id=btn2.GetId())
# Bind any button event to a callback routine
self.Bind(wx.EVT_BUTTON, self.on_a_button_press)
# button 1 pressed
def on_button1_press(self, event):
print("You pressed button 1")
event.Skip()
# button 2 pressed
def on_button2_press(self, event):
print("You pressed button 2")
event.Skip()
# Any button pressed
def on_a_button_press(self, event):
print("You pressed a button")
event.Skip()
class MyFrame(wx.Frame):
def __init__(self):
super().__init__(parent=None, title="Hello wxPython")
panel = MyPanel(self)
self.Show()
if __name__ == "__main__":
app = wx.App(redirect=False)
frame = MyFrame()
app.MainLoop()

Related

wxpython: pause code as long as second frame is open

I'm pretty much a Python beginner and have only recently started with wxpython and now have the following question:
I'm writing an application where one window is open and by clicking a button, the user opens a second window, where they're supposed to input their name and click another button. My problem is that I don't know how to pause the 'event code' that is executed by clicking Button1 until the user is done with their actions in the second window.
Is there a way to pause the code in OnButton1 (after the line secondframe.Show() until the user has input their name in the SecondFrame and clicked on OnButton2?
Essentially my goal is that first, the name of the user is printed and only afterwards, Done is printed
class MainFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent)
self.panel = wx.Panel(self)
self.Button1 = wx.Button(self.panel, label='Button 1', pos=(20, 10), size=(90, 25))
#Set event handlers
self.Button1.Bind(wx.EVT_BUTTON, self.OnButton1)
def OnButton1(self, e):
secondframe = SecondFrame(None)
secondframe.Show()
print("Done")
class SecondFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent)
self.panel = wx.Panel(self)
self.Button2 = wx.Button(self.panel, label='Button 2', pos=(50, 20), size=(90, 25))
self.NameBox = wx.TextCtrl(self.panel, value="Type Your Name", pos=(50, 60), size=(200, -1))
#Set event handlers
self.Button2.Bind(wx.EVT_BUTTON, self.OnButton2)
def OnButton2(self, e):
Name = self.NameBox.GetValue()
print(Name)
app = wx.App(False)
frame = MainFrame(None)
frame.Show()
app.MainLoop()
I don't know of a way to actually block the code there. (Also, blocking is generally speaking never a good idea.)
But you can get the same behaviour like this:
Where you open the second window, in the first Window you call self.Disable()
When creating the second window, you also hand a reference to the first window to it (i.e. secondframe = SecondFrame(None, self)), adjust the constructor (def __init__(self, parent, main):) and store this in a variable there (self.mainFrame = main).
You add a method to MainFrame, e.g. reenable(self)
there you call self.Enable()
When you're done with Frame two, you can simply call self.mainFrame.reenable()
All in all, that would make something like:
class MainFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent)
self.panel = wx.Panel(self)
self.Button1 = wx.Button(self.panel, label='Button 1', pos=(20, 10), size=(90, 25))
#Set event handlers
self.Button1.Bind(wx.EVT_BUTTON, self.OnButton1)
def OnButton1(self, e):
secondframe = SecondFrame(None, self)
self.Disable()
secondframe.Show()
def reenable(self):
self.Enable()
print("done")
class SecondFrame(wx.Frame):
def __init__(self, parent, main):
wx.Frame.__init__(self, parent)
self.mainFrame = main
self.panel = wx.Panel(self)
self.Button2 = wx.Button(self.panel, label='Button 2', pos=(50, 20), size=(90, 25))
self.NameBox = wx.TextCtrl(self.panel, value="Type Your Name", pos=(50, 60), size=(200, -1))
#Set event handlers
self.Button2.Bind(wx.EVT_BUTTON, self.OnButton2)
def OnButton2(self, e):
Name = self.NameBox.GetValue()
print(Name)
self.mainFrame.reenable()
app = wx.App(False)
frame = MainFrame(None)
frame.Show()
app.MainLoop()

wxpython open second frame on mouse click no responding

env:python37,windows,wxpython
There's a main frame keep opening while app is running, now I'm trying to create a new frame on mouse right click, the new frame works good if I open it by button click, but if it is triggered by a mouse click event, it will hang and no responding. I'm thinking if there's something wrong with mouse listener, really appreciate if you have any idea.
here's the code detail:
import wx
import time
import win32api
from threading import Thread
class OtherFrame(wx.Frame):
"""
Class used for creating frames other than the main one
"""
def __init__(self, title, parent=None):
wx.Frame.__init__(self, parent=parent, title=title)
panel = wx.Panel(self)
panel.SetBackgroundColour('yellow')
self.Show()
class MyPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
btn = wx.Button(self, label='Create New Frame')
btn.Bind(wx.EVT_BUTTON, self.on_new_frame)
self.frame_number = 1
thread = Thread(target=self.monitorMouse, name='monitorMouse')
thread.daemon = True
thread.start()
def monitorMouse(self):
state_left = win32api.GetKeyState(0x01) # Left button down = 0 or 1. Button up = -127 or -128
state_right = win32api.GetKeyState(0x02) # Right button down = 0 or 1. Button up = -127 or -128
while True:
a = win32api.GetKeyState(0x01)
b = win32api.GetKeyState(0x02)
if b != state_right: # Button state changed
state_right = b
if b < 0:
print('Right Button Pressed')
else:
print('Right Button Released')
self.on_new_frame(None)
time.sleep(0.001)
def on_new_frame(self, event):
title = 'SubFrame {}'.format(self.frame_number)
frame = OtherFrame(title=title)
self.frame_number += 1
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title='Main Frame', size=(800, 600))
panel = MyPanel(self)
self.Show()
if __name__ == '__main__':
app = wx.App(False)
frame = MainFrame()
app.MainLoop()
If are determined to monitor the mouse using a thread, then I think that you are going to have to look at wx.lib.newevent.NewEvent().
From within the thread you would use wx.PostEvent to send the mouse event back to the main program.
However, wxpython already has the mouse events available to you, to which you can bind directly e.g.
import wx
class OtherFrame(wx.Frame):
def __init__(self, title, parent=None):
wx.Frame.__init__(self, parent=parent, title=title)
panel = wx.Panel(self)
panel.SetBackgroundColour('yellow')
self.Show()
class MyPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
btn = wx.Button(self, label='Create New Frame')
btn.Bind(wx.EVT_BUTTON, self.on_new_frame)
self.Bind(wx.EVT_LEFT_DOWN, self.Left)
self.Bind(wx.EVT_RIGHT_DOWN, self.Right)
self.Bind(wx.EVT_RIGHT_DCLICK, self.on_new_frame)
self.Bind(wx.EVT_CLOSE, self.Quit)
self.frame_number = 1
def Quit(self, event):
self.Destroy()
def Left(self, event):
print("Left Button")
event.Skip()
def Right(self, event):
print("Right Button")
def on_new_frame(self, event):
title = 'SubFrame {}'.format(self.frame_number)
frame = OtherFrame(parent=self,title=title)
self.frame_number += 1
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title='Main Frame', size=(800, 600))
panel = MyPanel(self)
self.Show()
if __name__ == '__main__':
app = wx.App(False)
frame = MainFrame()
app.MainLoop()
As an aside, note the line
frame = OtherFrame(parent=self,title=title)
sending the parent allows all outstanding OtherFrame's to close when the main frame is closed.

Stop a wx.Control from losing focus on arrow key press?

A pretty minimal example:
import wx
class Control(wx.Control):
def __init__(self, parent):
wx.Control.__init__(self, parent)
self.Bind(wx.EVT_CHAR, self.OnKey)
self.Bind(wx.EVT_KEY_DOWN, self.OnKey)
self.Bind(wx.EVT_KEY_DOWN, self.OnKey)
self.Bind(wx.EVT_LEFT_DOWN, self.OnMouseClick)
def OnKey(self, event):
print("key pressed")
event.Skip()
def OnMouseClick(self, event):
self.SetFocus()
print("has focus")
event.Skip()
class Frame(wx.Frame):
def __init__(self, parent=None):
wx.Frame.__init__(self, parent)
panel = wx.Panel(self)
sizer = wx.BoxSizer(wx.HORIZONTAL)
radio = wx.RadioButton(panel, label="Radio button")
button = wx.Button(panel, label="Button")
control = Control(panel)
sizer.Add(radio, 0, wx.ALL, 5)
sizer.Add(button, 0, wx.ALL, 5)
sizer.Add(control, 0, wx.ALL, 5)
panel.SetSizer(sizer)
self.Show()
if __name__ == "__main__":
app = wx.App()
frame = Frame()
app.MainLoop()
By clicking on the control, sets the focus to control. However, even though binding key presses to a function OnKey, an arrow key press changes focus to another button/widget.
Is there a method similar to AcceptsFocusFromKeyboard(self):
Description: Can this window be given focus by keyboard navigation? if not, the only way to give it focus (provided it accepts it at all) is to click it.
Except, a method where my control doesn't lose focus from keyboard navigation?
Both of you event handlers are missing the call to:
event.Skip()
Those events are non-wxCommandEvents and in order for them to work properly, OS has to perform whatever needs to be performed. That's what Skip() is for. Otherwise the event will be eaten by the control.
Please check the documentation for that method, add it and see if that fixes the issue.
Also, out of curiosity, what do you expect to happen when the user presses the arrow key?
If you want the focus to stay when the arrow key is pressed, try to filter the event by event.GetKeyCode() (check the docs for the proper function name) and don't call event.Skip() in this case.
If that doesn't work try to catch wx.EVT_CHAR_HOOK event.
If even this doesn't work, override FilterEvent().
Also, the structure of you GUI is very weird. Try to get the RAD tool (wxGlade, wxSmith, etc), build it there and look at the result. Or check the demo folder in wxPython distribution (available as a different download) to see how to make the normal layout in wxPython.
It is also possible that by fixing the GUI you will fix the arrow key issue.
Try this code instead:
import wx
class Frame(wx.Frame):
def __init__(self, parent=None):
wx.Frame.__init__(self, parent)
panel = wx.Panel(self)
sizer = wx.BoxSizer(wx.HORIZONTAL)
radio = wx.RadioButton(panel, label="Radio button")
button = wx.Button(panel, label="Button")
self.Bind(wx.EVT_CHAR, self.OnChar)
radio.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
button.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
self.Bind(wx.EVT_LEFT_DOWN, self.OnMouseClick)
sizer.Add(radio, 0, wx.ALL, 5)
sizer.Add(button, 0, wx.ALL, 5)
sizer.Add(control, 0, wx.ALL, 5)
panel.SetSizer(sizer)
self.Show()
def OnKeyDown(self, event):
print("key pressed")
if not event.GetKeyPress() == wx.WXK_LEFT_ARROW: # Check the function name. I'm writing from memory
event.Skip()
def OnChar(self, event):
print("Character key pressed on the panel")
event.Skip()
def OnMouseClick(self, event):
self.SetFocus()
print("has focus")
event.Skip()
if __name__ == "__main__":
app = wx.App()
frame = Frame()
app.MainLoop()
You should get the idea. Call event.Skip() only if the key pressed is not an arrow key.

AddPendingEvent has no effect

I want to send events using AddPendingEvent. However, nothing happens after calling AddPendingEvent. The following is an example, where a button is expected to send a wx.CloseEvent to the frame.
import wx
class MainFrame(wx.Frame):
def __init__(self):
super(wx.Frame, self).__init__(None, wx.ID_ANY, 'Test')
self.button = wx.Button(self, wx.ID_ANY, 'Close', self.GetClientSize()/2)
self.button.Bind(wx.EVT_BUTTON, self.OnButton)
self.Bind(wx.EVT_CLOSE, self.OnClose)
self.Show()
def OnButton(self, event: wx.CommandEvent):
self.AddPendingEvent(wx.CloseEvent())
def OnClose(self, event: wx.CloseEvent):
self.Destroy()
if __name__ == '__main__':
app = wx.App()
frame = MainFrame()
app.MainLoop()
I have also tried QueueEvent or wx.PostEvent, the results are the same.
You should go by PyCommandEvent and create & enqueue an event of type wx.EVT_CLOSE as in:
self.AddPendingEvent(wx.PyCommandEvent(wx.EVT_CLOSE.typeId, self.GetId()))

wxpython: Why do I enter the binding function twice?

The code is only used to write a simple UI( there is only a textbox on the window), and bind the event wx.EVT_KEY_DOWN to the function OnKeyDown, but when I pressed the Esc key, the window will pop up Esc, Test, then another Esc, Test, finally it will exit after the four message box, why? I only define two message box int the wx.WXK_ESCAPE bindings.
# -*- coding: utf-8 -*-
import wx
class Command(wx.Frame):
def __init__(self, parent, title):
super(Command, self).__init__(parent, title=title,
size=(600, 500))
self.InitUI()
self.Centre()
self.Show()
def InitUI(self):
pnl = wx.Panel(self)
self.Bind(wx.EVT_CHAR_HOOK, self.OnKeyDown)
hbox = wx.BoxSizer(wx.HORIZONTAL)
self.__tc_command = wx.TextCtrl(pnl, style=wx.TE_MULTILINE)
self.Bind(wx.EVT_CHAR_HOOK, self.OnKeyDown)
hbox.Add(self.__tc_command, proportion=1, flag=wx.ALL|wx.EXPAND, border=15)
pnl.SetSizer(hbox)
def OnKeyDown(self, evt):
"""Enter to send data, Esc to exit."""
key = evt.GetKeyCode()
if key == wx.WXK_ESCAPE:
##################Only two MessageBox, but pop up four##################
wx.MessageBox("Esc")
wx.MessageBox("Test")
##################Only two MessageBox, but pop up four##################
self.Close()
if key == wx.WXK_RETURN:
wx.MessageBox("Enter")
evt.Skip()
if __name__ == '__main__':
app = wx.App(redirect=False)
Command(None, title='Command')
app.MainLoop()
You are calling self.Bind(wx.EVT_CHAR_HOOK, self.OnKeyDown) twice in your code.

Categories

Resources