Python application launch position (location) on monitors [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have been given a project in python and I'm not too familiar with it but I understand the basics due other coding work I've done.
The task is to basically make the application launch in last location it was closed.
There is no save button and no config (xml / ini etc.) currently used so I would assume it would need to create one of these, if not existing, or update an existing one upon closure of the window.
Everyone has different size monitors / monitor layouts and resolutions so is there a module that can assess this and save the X,Y co-ords per user configuration?
Or is there a better way to do this using the modules listed below? Or do I need to import an additional module?
These are the current modules imported:
import os
import sys
import pygtk
pygtk.require('2.0')
import gtk
Any help would be greatly appreciated.
UPDATE:
Forgot to post an update:
I managed to get this working perfectly into the existing app using J Arun Mani method.
I used os module to define the local directory where the file should be written to / read from and boom it works flawless. Thanks again

You can use the methods, Gtk.Window.get_position and Gtk.Window.move to get and set coordinates of window. (Link to doc)
But be aware that the placement of windows is users' preference and is taken care by window manager. So normally, you should not mess with it.
A simple example to demonstrate what you wanted:
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
def window_start(win):
try:
fp = open("win_coords")
except FileNotFoundError:
print("No file !")
return
coords = fp.read()
x, y = eval(coords) # Don't use eval if you can't trust the file
print("Moving to ", coords)
win.move(x, y)
fp.close()
def window_close(win, event):
fp = open("win_coords", "w")
coords = tuple(win.get_position())
print("Writing coordinates ", coords)
fp.write(str(coords))
fp.close()
win.destroy()
Gtk.main_quit()
label = Gtk.Label(label="Hello world")
win = Gtk.Window()
win.add(label)
win.connect("delete-event", window_close) # Connect to get the coordinates
win.show_all()
window_start(win)
Gtk.main()

Related

Pyautogui in specific screen app in 2 monitors

Well, let's separate this doubt into parts.
First question, how can I make pyautogui.locateOnScreen() in a specific app window on windows? Example, search for an image only in the windows calculator?
The second question is I have 2 monitors, how do I search for an image on a specific monitor?
I make a simple code, but not working because my calculator is opened on my second monitor.
def main():
try:
while True:
button7location = pyautogui.locateOnScreen('images/calc7Key.png', region=(0,0,1920, 1080), confidence=.5)
print(button7location)
except KeyboardInterrupt:
print('\nDone.')
main()
Unfortunately pyautogui currently doesn't work with multiple monitors, you can find it in their FAQ
Q: Does PyAutoGUI work on multi-monitor setups.
A: No, right now PyAutoGUI only handles the primary monitor.
As of searching specific area you can use optional region=(startXValue,startYValue,width,height) parameter as shown here.
this answer may be late but for those looking, the answer is here :
https://github.com/asweigart/pyautogui/issues/321
quote - Resolve for me
If still relevant for someone on windows:
In my opinion the issue is, that the current version of pyscreeze
utilizing >ImageGrab (Pillow) on windows only uses single-screen grab.
A dirty quick fix in pyscreeze could be:
enable all_screen grabbing:
In file: pyscreeze/__init__.py, function: def _screenshot_win32(imageFilename=None, region=None):
change im = ImageGrab.grab() to im = ImageGrab.grab(all_screens= True)
handle new introduced negative coordinates due to multiple monitor:
In file: pyscreeze/__init__.py, function: def locateOnScreen(image, minSearchTime=0, **kwargs): behind retVal = locate(image, screenshotIm, **kwargs) >add
if retVal and sys.platform == 'win32':
# get the lowest x and y coordinate of the monitor setup
monitors = win32api.EnumDisplayMonitors()
x_min = min([mon[2][0] for mon in monitors])
y_min = min([mon[2][1] for mon in monitors])
# add negative offset due to multi monitor
retVal = Box(left=retVal[0] + x_min, top=retVal[1] + y_min, width=retVal[2],height=retVal[3])
don't forget to add the import win32api
In file: pyscreeze/__init__.py:
if sys.platform == 'win32': # TODO - Pillow now supports ImageGrab on macOS.
import win32api # used for multi-monitor fix
from PIL import ImageGrab

Pyqt5 qlabel update while button is running [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
When the button is pressed, I want the pictures in the selected folder path to be displayed at 1 second intervals. But the interface is not updated until the method I connected the button to finishes. What can I do?
def analyze_button_clicked(self):
for (root,dirs,files) in os.walk(self.input_output_tree.topLevelItem(0).text(0)):
for file in files:
if file.endswith(".jpg") or file.endswith(".jpeg"):
image_path = os.path.join(root, file)
pixmap = QPixmap(image_path)
self.process_image.setPixmap(pixmap)
It will likely update if you call QApplication.processEvents() after updating the Pixmap, like so:
from PyQt5.QtWidgets import *
def analyze_button_clicked(self):
for (root,dirs,files) in os.walk(self.input_output_tree.topLevelItem(0).text(0)):
for file in files:
if file.endswith(".jpg") or file.endswith(".jpeg"):
image_path = os.path.join(root, file)
pixmap = QPixmap(image_path)
self.process_image.setPixmap(pixmap)
QApplication.processEvents()
However, like Cristian said, it is advisable to use Signals & Slots

How to replace all the print() calls by a LineEdit.setText() so the user only has to look at the application GUI and not the IDE's console? [duplicate]

This question already has answers here:
How to capture output of Python's interpreter and show in a Text widget?
(5 answers)
Closed 4 years ago.
I wrote a program to perform measurements and currently I launch it via the Spyder IDE. The program is written in Python 3.6.3. The GUI was made using PyQt5 and it should be the main focus of the user, but I also print() many informations in Spyder's console.
In preparation for switching to an .exe instead of a .py, since there will be no console anymore, I would like to add a LineEdit to my interface where all the printing would occur. Ideally it would display both my print()s and the various error messages generated during execution. How do I redirect those prints to a LineEdit?
Most of the information I found during my research was about making a LineEdit some kind of Windows cmd equivalent but examples were overkill compared to what I'm trying to do. Thanks in advance.
A quick and dirty method is to just redefine the builtin print function.
from PyQt5 import QtWidgets
IS_EXE = True # There is probably a way to detect this at runtime
class Window(QtWidgets.QPlainTextEdit):
def __init__(self):
super(Window, self).__init__()
self.setReadOnly(True)
if __name__ == '__main__':
app = QtWidgets.QApplication([])
win = Window()
win.show()
# Replace the builtin print function
if IS_EXE:
print = win.appendPlainText
# Print some stuff
print('foo')
print('bar')
app.exec_()

Import image as plane in blender script

Import images as planes work great in blender GUI, but when I try to use it in python module, I've got this error:
RuntimeError: Operator bpy.ops.mesh.primitive_plane_add.poll() Missing 'window' in context
My code is:
import bpy
import addon_utils
# enable plugins
addon_utils.enable("io_import_images_as_planes")
# remove Cube object
bpy.data.objects['Cube'].select = True
bpy.ops.object.delete()
file = "test.jpg"
bpy.ops.import_image.to_plane(files=[{'name':file}], directory='.')
The import images as planes operator only functions within the 3dview, the current context is the window under the cursor when the script is run which would be the text editor where the script is. It is possible to override the current context, read this answer for more info.
Another option is to put your code into an operator and either run it by searching in the spacebar menu or from a button you add to the sidebar in the 3dview. You can find a template for creating a simple operator in blenders text editor or view it online.

How to embed some application window in my application using any Python GUI framework

I want some application to look like widget inside my Python application.
That's all. I dont need any interaction between them. I'm interested in solutions in any GUI toolkit for both windows and x windows.
It would be nice to have a solution with Tkinter but it's not crucial.
Using GTK on X windows (i.e. Linux, FreeBSD, Solaris), you can use the XEMBED protocol to embed widgets using gtk.Socket. Unfortunately, the application that you're launching has to explicitly support it so that you can tell it to embed itself. Some applications don't support this. Notably, I can't find a way to do it with Firefox.
Nonetheless, here's a sample program that will run either an X terminal or an Emacs session inside a GTK window:
import os
import gtk
from gtk import Socket, Button, Window, VBox, HBox
w = Window()
e = Button("Emacs")
x = Button("XTerm")
s = Socket()
v = VBox()
h = HBox()
w.add(v)
v.add(s)
h.add(e)
h.add(x)
v.pack_start(h, expand=False)
def runemacs(btn):
x.set_sensitive(False); e.set_sensitive(False)
os.spawnlp(os.P_NOWAIT, "emacs",
"emacs", "--parent-id", str(s.get_id()))
def runxterm(btn):
x.set_sensitive(False); e.set_sensitive(False)
os.spawnlp(os.P_NOWAIT, "xterm",
"xterm", "-into", str(s.get_id()))
e.connect('clicked', runemacs)
x.connect('clicked', runxterm)
w.show_all()
gtk.main()
Not enough reputation to comment on Glyphs answer. To make xterm work, in addition to the comments above one needs to also add
XTerm*allowSendEvents: True
to ~/.Xresources. (and perhaps reload those, with xrdb -load ~/.Xresources)

Categories

Resources