I am trying to create simple program which will click to specific coordinates every x seconds based on your choose if you need to click on Left screen or Right screen. My issue here is that win32api.SetCursorPos which is moving with my cursor is not moving to the secondary screen (right in my case). It stays in the main screen.
And I am having one more issue with my code, when Exit button inside GUI is pressed, window will close however program is still running in background. I am using self.Destroy() function to kill all process.
Thank you for your advice.
Here is my code:
import time
import pyautogui
import wx
import threading
import sys
import win32api
class bucky(wx.Frame):
def __init__(self,parent,id):
self.positionx = ""
self.positiony = ""
wx.Frame.__init__(self,parent,id,'AutoClick 2.0', size=(300,200))
panel=wx.Panel(self)
self.buttonpos=wx.Button(panel,label="Left Screen",pos=(30,10),size=(80,40))
self.buttonpos2=wx.Button(panel,label="Right Screen",pos=(180,10),size=(80,40))
self.button=wx.Button(panel,label="Start",pos=(120,90),size=(60,30))
self.button2=wx.Button(panel,wx.ID_EXIT,label="Exit",pos=(120,120),size=(60,30))
self.Bind(wx.EVT_BUTTON, self.action, self.button)
self.Bind(wx.EVT_BUTTON, self.closebutton, self.button2)
self.Bind(wx.EVT_BUTTON, self.position, self.buttonpos)
self.Bind(wx.EVT_BUTTON, self.position, self.buttonpos2)
self.Bind(wx.EVT_CLOSE, self.closewindow)
def position(self, event):
label = event.GetEventObject().GetLabel()
if label == "Left Screen":
self.positionx = 1640
self.positiony = 183
self.buttonpos.Disable()
self.buttonpos2.Enable()
elif label == "Right Screen":
self.positionx = 3308
self.positiony= 186
self.buttonpos.Enable()
self.buttonpos2.Disable()
def closebutton(self,event):
self.Destroy()
def closewindow(self,event):
self.Destroy()
def action(self,event):
self.button.Disable()
def callback():
while 1:
pos = pyautogui.position()
time.sleep(10)
pos1 = pyautogui.position()
if (pos1[0] == pos[0]) and (pos1[1] == pos[1]):
win32api.SetCursorPos((self.positionx, self.positiony))
pyautogui.click()
else:
pass
t = threading.Thread(target=callback)
t.start()
if __name__=='__main__':
app=wx.PySimpleApp()
frame=bucky(parent=None,id=1)
frame.Show()
app.MainLoop()
EDIT: Problem have been solved. Thank you for help.
Looking at this reddit post, I think the x coordinates for the secondary monitor are simply added to the resolution of the primary monitor (ex if you had two 1920x1080 monitors, the middle of the second monitor will be at 2880,520)
Try using (win32api.GetSystemMetrics(MONITOR_NUMBER) to see how the differences are represented.
Related
I am using wxpython...How can I detect and perform a function if someone clicks the Red 'X' on the top right corner ( Close button )? What is the code? Can someone please help me? Thanks!
You are looking for EVT_CLOSE.
e.g.
import wx
class Test(wx.Frame):
def __init__(self,parent):
wx.Frame.__init__(self,parent,title="Main Window",size = (300,200))
panel = wx.Panel(self)
menubar=wx.MenuBar()
firstm=wx.Menu()
fm1 = wx.MenuItem(firstm, -1, 'Quit\tAlt+Q')
firstm.Append(fm1)
self.Bind(wx.EVT_MENU, self.OnExit, id=fm1.GetId())
# Catch Clicking on the Corner X to close
self.Bind(wx.EVT_CLOSE, self.OnExit)
menubar.Append(firstm,"File")
self.SetMenuBar(menubar)
t = wx.StaticText(panel,-1,"Testing 1 2 3 ....", pos=(10,20))
def OnExit(self, event):
# To discover how you got here,
# you can either test the event type or define a separate function for EVT_CLOSE,
# most of the time you don't care
if event.GetEventType() == wx.EVT_CLOSE.typeId:
print("Close using X")
else:
print("Close using Menu or Alt+Q")
self.Destroy()
if __name__=='__main__':
app=wx.App()
frame=Test(None)
frame.Show()
app.MainLoop()
I am developing a test bench, in which on clicking a button, a specific test will be done, and accordingly if test is done successfully, then it should be indicated by colored rectangle. But the problem is that how can I bind 2 events with one button clicking event. And also when i execute the attached code, open clicking the button, rectangle wont show up on panel.
import wx
class MyClass(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id, "GUI", size =(500,300))
panel=wx.Panel(self)
button=wx.Button(panel,label = 'Exit', pos = (250,180), size = (50,50))
button1=wx.Button(panel,label = 'Open', pos = (50,50), size = (50,50))
self.Bind(wx.EVT_PAINT, self.Rectangle,button1)
self.Bind(wx.EVT_BUTTON,self.CloseButton,button)
self.Bind(wx.EVT_CLOSE,self.CloseProgram)
def Rectangle(self,e):
dc = wx.PaintDC(self.panel)
dc.SetPen(wx.Pen('RED'))
dc.SetBrush(wx.Brush('BLUE'))
dc.DrawRectangle(130, 15, 90, 60)
def CloseButton(self,e):
self.Close(True)
def CloseProgram(self,e):
self.Destroy()
if __name__ == '__main__':
app=wx.App()
frame=MyClass(parent=None,id=-1)
frame.Show()
app.MainLoop()
I want to pause the code at exact position and wait for the different input plus the start button clicked. However, if it is not achievable, how can I add another button into the start button to make it work?
import wx
import time
import RPi.GPIO as GPIO
global Total_Length
Total_Length = 500
global Input_Length
Input_Length = 0
a = 0
class tyler(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,'Lyquid Crystal Laser Control',size=(500,200))
panel=wx.Panel(self)
#global Text_1
self.Text_1 = wx.TextCtrl(panel,-1,"0",(350,30),(50,30))
self.Text_1.Bind(wx.EVT_TEXT_ENTER,self.Start)
self.Text_1.Bind(wx.EVT_KILL_FOCUS,self.Start)
self.timer = wx.Timer(self)
#self.Bind(wx.EVT_TIMER, self.timer)
button_1=wx.Button(panel,label="Start",pos=(400,80),size=(80,30))
button_2=wx.Button(panel,label="Stop",pos=(400,120),size=(80,30))
self.Bind(wx.EVT_BUTTON, self.Start, button_1)
#self.Bind(wx.EVT_BUTTON, self.Stop, button_2)
def Start(self,event):
global a
Input_Length=float(self.Text_1.GetValue())
#print(Input_Length)
#a = Input_Length
#print(Input_Length)
dc=float(100*Input_Length/Total_Length)
GPIO.setmode(GPIO.BCM)
GPIO.setup(18,GPIO.OUT)
GPIO.setwarnings(False)
p = GPIO.PWM(18,1150)
p.start(0)
p.ChangeDutyCycle(dc)
p.ChangeFrequency(1150)
#I wanted to pause the code at here, until the input changes, and the start button clicked, so I add timer in below, however, the output is only a pulse but the square wave is what I wanted
if a == dc:
self.timer.Start(1000)
else:
a = dc
self.timer.Stop()
#def Stop(self,event):
GPIO.cleanup()
if __name__=='__main__':
app=wx.PySimpleApp()
frame=tyler(parent=None,id=-1)
frame.Show()
app.MainLoop()
"Pause and wait" and "event-driven GUI programming" do not go together. As long as the main GUI thread is blocked waiting for something, then other events can't be processed and the program will appear to be frozen. Your options are to change how you "wait" (by not actually waiting) or to use another thread.
This answer to another question applies just as well here, and will give you more information and pointers.
Hi Iam using Python and GTK+. In my GUI I have 2 toolbars I want show first toolbar only if user moves mouse than hide it again after few seconds as for second toolbar I want to show it when user is on particular x,y coordinates.How can I achieve it ?
EDIT:
Iam creating some kind of media player so I want toolbars to disapear while user is not using mouse in case of playerMenu toolbar or if user doesn't move it to specific location in case of ribbonBar toolbar .Iam using GTK+ here is my code for toolbars:
class Player(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
def build_UI(self):
container=Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
ribbonBar=Gtk.Toolbar()
playerMenu=Gtk.Toolbar()
def mouse_moved(self):
#TO-DO here I should check cordinates for example I want to see if mouse.y=window.height-50px and I would like to show ribbonaBar
#after that Gdk.threads_add_timeout(1000,4000,ribbonBar.hide)
#TO-DO here I show playerMenu toolbar if mouse is moved
# smt like playerMenu.show()
#after that I would call Gdk.threads_add_timeout(1000,4000,playerMenu.hide)
# to hide it again after 4 seconds
I should connect my window to some mouse event but I don't know the event name and how can I get mouse.x and mouse.y?
Why do you want to do this? Trying to use widgets that disappear when you're not moving the mouse is rather annoying, IMHO.
But anyway...
To toggle the visibility of a widget use the show() and hide() methods, or map() and unmap() if you don't want the other widgets in your window to move around. To handle timing, use gobject.timeout_add(), and you'll need to connect() your window to "motion_notify_event" and set the appropriate event masks: gtk.gdk.POINTER_MOTION_MASK and probably gtk.gdk.POINTER_MOTION_HINT_MASK. The Event object that your motion_notify callback receives will contain x,y mouse coordinates.
At least, that's how I'd do it in GTK2; I don't know GTK3.
If you want more specific help you need to post some code.
I see that you've posted some code, but it doesn't have a lot of detail... But I understand that GTK can be a bit overwhelming. I haven't used it much in the last 5 years, so I'm a bit rusty, but I just started getting into it again a couple of months ago and thought your question would give me some good practice. :)
I won't claim that the code below is the best way to do this, but it works. And hopefully someone who is a GTK expert will come along with some improvements.
This program builds a simple Toolbar with a few buttons. It puts the Toolbar into a Frame to make it look nicer, and it puts the Frame into an Eventbox so we can receive events for everything in the Frame, i.e., the Toolbar and its ToolItems. The Toolbar only appears when the mouse pointer isn't moving and disappears after a few seconds, unless the pointer is hovering over the Toolbar.
This code also shows you how to get and process mouse x,y coordinates.
#!/usr/bin/env python
''' A framed toolbar that disappears when the pointer isn't moving
or hovering in the toolbar.
A response to the question at
http://stackoverflow.com/questions/26272684/how-can-i-show-hide-toolbar-depending-on-mouse-movements-and-mouse-position-insi
Written by PM 2Ring 2014.10.09
'''
import pygtk
pygtk.require('2.0')
import gtk
import gobject
if gtk.pygtk_version < (2, 4, 0):
print 'pygtk 2.4 or better required, aborting.'
exit(1)
class ToolbarDemo(object):
def button_cb(self, widget, data=None):
#print "Button '%s' %s clicked" % (data, widget)
print "Button '%s' clicked" % data
return True
def show_toolbar(self, show):
if show:
#self.frame.show()
self.frame.map()
else:
#self.frame.hide()
self.frame.unmap()
def timeout_cb(self):
self.show_toolbar(self.in_toolbar)
if not self.in_toolbar:
self.timer = False
return self.in_toolbar
def start_timer(self, interval):
self.timer = True
#Timer will restart if callback returns True
gobject.timeout_add(interval, self.timeout_cb)
def motion_notify_cb(self, widget, event):
if not self.timer:
#print (event.x, event.y)
self.show_toolbar(True)
self.start_timer(self.time_interval)
return True
def eventbox_cb(self, widget, event):
in_toolbar = event.type == gtk.gdk.ENTER_NOTIFY
#print event, in_toolbar
self.in_toolbar = in_toolbar
#### self.show_toolbar(in_toolbar) does BAD things :)
if in_toolbar:
self.show_toolbar(True)
return True
def quit(self, widget): gtk.main_quit()
def __init__(self):
#Is pointer over the toolbar Event box?
self.in_toolbar = False
#Is pointer motion timer running?
self.timer = False
#Time in milliseconds after point stops before toolbar is hidden
self.time_interval = 3000
self.window = win = gtk.Window(gtk.WINDOW_TOPLEVEL)
width = gtk.gdk.screen_width() // 2
height = gtk.gdk.screen_height() // 5
win.set_size_request(width, height)
win.set_title("Magic Toolbar demo")
win.set_border_width(10)
win.connect("destroy", self.quit)
#self.motion_handler = win.connect("motion_notify_event", self.motion_notify_cb)
win.connect("motion_notify_event", self.motion_notify_cb)
win.add_events(gtk.gdk.POINTER_MOTION_MASK |
gtk.gdk.POINTER_MOTION_HINT_MASK)
box = gtk.VBox()
box.show()
win.add(box)
#An EventBox to capture events inside Frame,
# i.e., for the Toolbar and its child widgets.
ebox = gtk.EventBox()
ebox.show()
ebox.set_above_child(True)
ebox.connect("enter_notify_event", self.eventbox_cb)
ebox.connect("leave_notify_event", self.eventbox_cb)
box.pack_start(ebox, expand=False)
self.frame = frame = gtk.Frame()
frame.show()
ebox.add(frame)
toolbar = gtk.Toolbar()
#toolbar.set_border_width(5)
toolbar.show()
frame.add(toolbar)
def make_toolbutton(text):
button = gtk.ToolButton(None, label=text)
#button.set_expand(True)
button.connect('clicked', self.button_cb, text)
button.show()
return button
def make_toolsep():
sep = gtk.SeparatorToolItem()
sep.set_expand(True)
#sep.set_draw(False)
sep.show()
return sep
for i in xrange(5):
button = make_toolbutton('ToolButton%s' % (chr(65+i)))
toolbar.insert(button, -1)
#toolbar.insert(make_toolsep(), -1)
for i in xrange(1, 9, 2):
toolbar.insert(make_toolsep(), i)
button = gtk.Button('_Quit')
button.show()
box.pack_end(button, False)
button.connect("clicked", self.quit)
win.show()
frame.unmap()
def main():
ToolbarDemo()
gtk.main()
if __name__ == "__main__":
main()
I am building a simple signal generator in Python based on the Pyo and WX libraries.
I have ran through the simple tutorials for each and have successfully bound buttons in WX to WX functions. I am now trying to generate a simple sine wave(at 440 hz) for 1 second by pressing the button labeled "Oscillator 1"; however, when the main() function executes, the sine tone is played and while the button is displayed in the wx frame I am unable to retrigger the sine tone. Both of these symptoms are unwanted.
Why does the sine tone play immediately on program execution? Why does the firstOSC button seemingly not work?
import wx
from pyo import *
import time
pyoServer = Server().boot()
pyoServer.start()
class MainWindow(wx.Frame):
def __init__(self,parent,title):
wx.Frame.__init__(self,parent,title=title, size = (640,640))
self.CreateStatusBar() # A StatusBar in the bottom of the window
# Signal Generator controls
oscillator = SoundOutput()
firstOSC = wx.Button(self, wx.ID_YES,"Oscillator 1 " + str(oscillator.str_osc1State))
self.Bind(wx.EVT_BUTTON, oscillator.OnOff1(440), firstOSC)
#Menus
filemenu = wx.Menu()
menuExit = filemenu.Append(wx.ID_EXIT,"&Exit","Terminate the program")
menuBar = wx.MenuBar()
menuBar.Append(filemenu,"&File")
self.SetMenuBar(menuBar)
self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
self.Show(True)
def OnExit(self,e):
self.Close(True)
class SoundOutput(object):
def __init__(self):
self.osc1State = False
self.str_osc1State = "Off"
self.a = Sine(440, 0, 0.1)
def OnOff1(self, frequency):
self.a.freq = frequency
self.a.out()
time.sleep(1)
self.osc1State = True
def Main():
app = wx.App(False)
frame = MainWindow(None,"Signal Generator")
app.MainLoop()
I solved this by investigating how WX handles events. As it turns out, for some reason calling a method in a nested or separate instance of a class caused the tone to play at runtime instead of on the event. I fixed this by making a method for the MainWindow class that serves as the binded event handler for firstOSC. This method then calls the requisite methods for the actual oscillator class.
Here is the new code:
# Signal Generator controls
self.fOscillator = SoundOutput()
self.fOscillatorstatus = False
self.firstOSC = wx.Button(self, wx.ID_ANY,"Oscillator 1 On")
self.firstOSC.Bind(wx.EVT_BUTTON, self.OnFirstOSC)
def OnFirstOSC(self,e):
if not self.fOscillatorstatus:
self.fOscillator.OnOff1(440)
self.fOscillatorstatus = True
self.firstOSC.SetLabel("Oscillator 1 Off")
elif self.fOscillatorstatus:
self.fOscillator.OnOff1(0)
self.firstOSC.SetLabel("Oscillator 1 On")
self.fOscillatorstatus = False