Renaming windows with python Xlib - python

Using the Xlib module for python, I have managed to move, resize, and destroy windows by calling configure() and destroy() respectively followed by display.sync().
However, I can't seem to rename existing windows. The following code outputs 'foo':
window.set_wm_name('foo')
window.set_wm_icon_name('foo')
print window.get_wm_name()
which would suggest that the object updates properly. On my screen, though, the window's title doesn't change at all, even after calling display.sync(). Am I using the set_wm_name functions correctly, if they should be used at all for this sort of task?

I know im extremly late with this answer but maybe someone will find it usefull
from Xlib.display import Display
from Xlib.Xatom import STRING
display = Display()
root = display.screen().root
#view the current WM_NAME
a = root.get_full_property(display.intern_atom('_NET_WM_NAME'), STRING)
print a.value
#Actualy change WM_NAME
root.change_property(display.intern_atom('_NET_WM_NAME'), STRING, 8, ' MyApp')
#read the name again
b = root.get_full_property(display.intern_atom('_NET_WM_NAME'), STRING)
print b.value
I've been unable to get window.get_wm_name()...Im sure im doing something wrong, but this works just as well :)

Related

pywinauto with a popup window

Im working through pywinauto, not a great developer but I can write some of the basics in python. Im getting hung up though. I have a popup that is causing me to not be able to press ok on and really not sure what direction I need to look
Really a two part question:
How can I move over to this popup IF it occurs, this wont always be the case as sometimes those files may not exist.
I tried using app.Confirm.Ok.click() and also app.Confirm.type('{ENTER}') neither worked.
How can I add in variables that I could call from an external .txt file for things like the "localhost" I included there in my code?
Code here:
from pywinauto import application
import time
app = application.Application()
app.start("Install.exe")
app.SelectLanguage.Ok.click()
app.Platform.Iacceptthetermsinthelicenseagreement.click()
app.Platform.Next.click()
app.Platform.Next.click()
app.PlatformInstallationOptions.Next.click()
app.PlatformSpecifyCertifcate.comboBox.select(0)
app.PlatformSpecifyCertifcate.Next.click()
app.PlatformConfigurationDatabaseOptions.type_keys('{TAB}')
app.PlatformConfigurationDatabaseOptions.type_keys('{TAB}')
app.PlatformConfigurationDatabaseOptions.type_keys('localhost')
app.PlatformConfigurationDatabaseOptions.type_keys('{TAB}')
app.PlatformConfigurationDatabaseOptions.type_keys('{SPACE}')
app.PlatformConfigurationDatabaseOptions.type_keys('{TAB}')
app.PlatformConfigurationDatabaseOptions.type_keys('Config')
app.PlatformConfigurationDatabaseOptions.Next.click()
app.PlatformSpecifyHTTPSBindingCertifcate.type_keys('{TAB}')
app.PlatformSpecifyHTTPSBindingCertifcate.type_keys('{TAB}')
app.PlatformSpecifyHTTPSBindingCertifcate.type_keys('{RIGHT}')
app.PlatformSpecifyHTTPSBindingCertifcate.type_keys('{SPACE}')
app.PlatformSpecifyHTTPSBindingCertifcate.Next.click()
app.PlatformAdvancedWorkflowSettings.Next.click()
app.PlatformPlatformLanguage.Next.click()
app.PlatformInstanceDatabaseOptions.type_keys('{TAB}')
app.PlatformInstanceDatabaseOptions.type_keys('{TAB}')
app.PlatformInstanceDatabaseOptions.type_keys('localhost')
app.PlatformInstanceDatabaseOptions.type_keys('{TAB}')
app.PlatformInstanceDatabaseOptions.type_keys('{SPACE}')
app.PlatformInstanceDatabaseOptions.type_keys('{TAB}')
app.PlatformInstanceDatabaseOptions.type_keys('Instance')
app.PlatformInstanceDatabaseOptions.Next.click()
app.PlatformTimeZone.Next.click()
app.WebApplicationOptions.type_keys('{TAB}')
app.WebApplicationOptions.type_keys('{TAB}')
app.WebApplicationOptions.type_keys('{TAB}')
app.WebApplicationOptions.type_keys('{TAB}')
app.WebApplicationOptions.type_keys('{UP}')
app.WebApplicationOptions.Next.click()
app.WebApplicationOptions.type_keys('{ENTER}')
confirmWin = .app.window(title_re = u'Confirm') #Check your window header object name.
# Use timeout based on average pop up time in your application.
if confirmWin.exists(timeout=10, retry_interval=1):
confirmWin.set_focus()
yesBtn = confirmWin[u'&Yes']
# Check the object name of the Yes button. You can use Swapy tool(It is deprecated but it works, else you can use inspect.exe)
yesBtn.click()
else:
print('Confirmation pop up did not appear')
This should work :)

Changing Screen Resolution of Computer using python

I'm creating a python program that is supposed to streamline the process of setting up a computer. I want this python program to change the screen resolution of the computer and scaling of it. I'm not sure what the best approach is however, or how to approach it.
I've tried using an example pywin32 program, but it only outputted an array of resolution sizes
I had a look how to change screen resolution using C++ and then translated it to Python:
import win32api
import win32con
import pywintypes
devmode = pywintypes.DEVMODEType()
devmode.PelsWidth = 1366
devmode.PelsHeight = 768
devmode.Fields = win32con.DM_PELSWIDTH | win32con.DM_PELSHEIGHT
win32api.ChangeDisplaySettings(devmode, 0)
We needed a DEVMODE object to pass to the ChangeDisplaySettings function. The pywintypes module which is also part of pywin32 has a function to create objects of type DEVMODE.
We then set the PelsWidth and PelsHeight fields and also the Fields field to tell the API which field's values we want to use.
To change back to the previous resolution, simply call:
win32api.ChangeDisplaySettings(None, 0)
Thanks for asking the question. I've learned something.

How do I reset the image on a button in tkinter?

I've searched around on the Internet and couldn't seem to find a way to remove the image I placed on a button. I was wondering if there is a way to remove the image but keep the button or any other simple quick fix. Here's some of my code for reference.
def breakcup():
if firstroom.cupnotbroken:
messagebutton.config(text="You broke the cup, and the key was inside the cup.")
cup.config(image=photo4)
firstroom.cupnotbroken=False
else:
cup.config(image=None, state=DISABLED)
messagebutton.config(text="You picked up the key")
firstroom.keynotfound=False
Obviously, image=None does not work, but it was the closest thing I could find as a solution.
root = Toplevel(bob)
root.geometry("640x360+200+250")
root.resizable(0, 0)
app = Room1(root)
The windows are made using the Toplevel(parent) function. Just to clarify.
This appears to be a bug with Tkinter. From my experimentation it seems safe to set the image to an empty string rather than None:
messagebutton.configure(image="")
This works because in the underlying tcl/tk interpreter "everything is a string". That is, the tcl equivalent to None is "". When you specify an empty string, Tkinter passes that empty string to tcl, and tcl interprets it as "no image".

How do I get the Mac "command" symbol in a Tkinter menu

I've written a Python program for a friend's business, and am using Tkinter for the interface. Up until now, all features have been added in the main program window, but I'm now adding a print feature, have created a simple "File" menu, and want to add a "Print" entry to that menu, including displaying the relevant keyboard shortcut.
On my Mac, I want the shortcut to be Command-P. I found the Mac "command" symbol's Unicode value, and have tried various ways to create an accelerator string that simply concatenates that symbol and the letter "P", but nothing works. I get either the symbol or the letter to display in the menu next to "Print", but never both.
Here is the full line of code that adds the menu item, with the latest attempt at building the string (I believe I found this unicode.join option elsewhere in Stack Overflow):
sub_menu.add_command(label="Print", command=self.print_, accelerator=unicode.join(u"\u2318", u"P"))
// Only the "P" displays
Here are some of the other options that I've tried (lines truncated for clarity). With each of these options, only the "command" symbol appears:
accelerator=u"\u2318\u0050"
accelerator=u"\u2318" + "P"
accelerator=u"\u2318" + u"P"
accelerator=u"\u2318P"
accelerator=u"".join([u"\u2318", u"P"])
Up until now I haven't had a need to learn much about Unicode strings, so perhaps there's something I'm doing wrong in that regard. However, all of the attempts that I've made have come as a result of various searches, both here and elsewhere, and so far nothing has worked. Any insight into how to make this work would be most welcome!
Python 2.7.3, Mac OS X 10.8.3
After further searching online, I finally found a page that has the solution. I was surprised (and a touch annoyed) that this solution was different than the one outlined in the PDF documentation for Tkinter 8.4 that I've been referencing thus far (the one published by New Mexico Tech). I found links to Tkinter 8.5 documentation, but they also list the incorrect process.
Anyway, it's a lot simpler than I thought, and is similar to the syntax used for key binding, but slightly different. Instead of directly including the command symbol in the accelerator string, Tkinter takes the literal word "Command" (or the abbreviated "Cmd"), and internally converts it to the displayable character "⌘" in the menu. So my resulting line is:
sub_menu.add_command(label="Print", command=self.print_, accelerator="Command-P")
...and what I get in my full menu item is:
Print ⌘P
As the page linked above shows, similar shortcut words exist for other modifier keys, and on Mac OS X, these are all automatically translated into their graphical equivalents.
It was actually really easy, all I did was use string formatting to concatenate text="%s%s" % (u"\u2318","P")
Here is a sample Tkinter App, the display is small but it shows what you need.
import Tkinter as tk
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.label = tk.Label(text="%s%s" % (u"\u2318","P"))
self.label.pack(padx=10, pady=10)
app = SampleApp()
app.mainloop()
Output:
⌘P

Using Sikuli to take automated screenshots of a window?

Simple question here: I'd like to use Sikuli to take a screenshot of a window on a mac, which would be done by hitting CMD+SHIFT+4 then hitting Space, then clicking a window.
For the CMD+SHIFT+4 I'm having trouble. This doesn't work:
keyDown(KEY_META)
keyDown(Key.SHIFT)
wait(1)
type("4")
wait(1)
keyUp(Key.SHIFT)
keyUp(KEY_META)
Anyone have any ideas? I'm open to other routes of hitting the key combo, for instance, I know to copy this works well:
type("c",KEY_META)
But, it doesn't accept three arguments.
type("4", KeyModifier.CMD+KeyModifier.SHIFT)
Or, even better:
import shutil
import os
screenshotsDir = "absolute-path-to-a-folder"
img = capture(some_region)
shutil.move(img, os.path.join(screenshotsDir, "some-name.png"))
where some_region is:
some_region = SCREEN # for whole screen
or
someRegion = App.focusedWindow() # for the frontmost window
This has the advantage, that you can control the file name of the shot.
Have found a better solution, which actually works:
screen = Screen()
scr_img = screen.capture(screen.getBounds())
scr_img.save("C:\Screenshots", "screenshot")
Screen.capture() returns an instance of ScreenImage class with methods:'save', 'saveInBundle', 'getFile', 'getFilename'. The method save() adds an unique number to a supplied prefix parameter.

Categories

Resources