How to access popup menu from hidden system tray icon - python-3.x

I use following code to access hidden icon and to show its popup menu and to click on "Sign Off" menuitem:
import time
from pywinauto.application import Application
app = Application(backend="uia").connect(path="explorer.exe")
st = app.window(class_name="Shell_TrayWnd")
t = st.child_window(title="Notification Chevron").wrapper_object()
t.click()
time.sleep(0.25)
list_box = Application(backend="uia").connect(class_name="NotifyIconOverflowWindow")
list_box_win = list_box.window(class_name="NotifyIconOverflowWindow")
list_box_win.wait('visible', timeout=30, retry_interval=3)
list_box_win.child_window(title="Citrix Workspace").click_input(button='right').menu_item('Sign Out').click_input()
Popup menu is shown, but I got error: AttributeError: 'NoneType' object has no attribute 'menu_item'
When I tried to add this code (instead of .menu_item('Sign Out').click_input()) :
connector = Application()
connector.connect(path="C:\\Program Files (x86)\\Citrix\\ICA Client\\SelfServicePlugin\\SelfService.exe")
connector.PopupMenu.MenuItem("Sign Out").ClickInput()
path is reflecting application that responsible for icon in system tray.
I got error: MatchError: Could not find 'PopupMenu' in 'dict_keys([])'. So I can I access menuitem in popup window?
BTW: It is run on windows 10 64 bit with python 3.6.

Unpolished/optimized (but working) code looks like this:
import pywinauto.application
import time
taskbar = pywinauto.application.Application(backend="uia").connect(path="explorer.exe")
windows_tray = taskbar.window(class_name="Shell_TrayWnd")
tray = windows_tray.child_window(title="Notification Chevron").wrapper_object()
tray.click()
time.sleep(1)
app_list_box = pywinauto.application.Application(backend="uia").connect(class_name="NotifyIconOverflowWindow")
app_list_box_win = app_list_box.window(class_name="NotifyIconOverflowWindow")
target_app = app_list_box_win.child_window(title = "Citrix Workspace")
target_app.click_input(button = "right")
citrix = pywinauto.application.Application().connect(path='Receiver.exe')
sign = citrix.PopupMenu.menu().get_menu_path("Sign In...")[0]
sign.click_input()
Where 'Receiver.exe' is executable of application running in system tray and "Citrix Workspace" title of of it.

Related

How can I get a Windows 11 Context Menu item to work with my PyQt6 application

I have a python script that I would like to run from the Windows 11/10 Right Click Context Menu in my documents folder
"C:\\Users\\me\\Documents\\PythonScripts\\main.py"
from PyQt6.QtWidgets import QApplication, QWidget
app = QApplication([])
window = QWidget()
window.setWindowTitle('My PyQt6 Application')
window.resize(400, 300)
window.move(100, 100)
window.show()
app.exec()
I created a python script that adds an item to the Right Click Context Menu in Windows 11 via registry.
But, The problem I am having is that when I try to click on "My Python Script" in the context menu, a pyqt6 window should pop up.
I tried running the exact command in terminal window
C:\Users\me\AppData\Local\Programs\Python\Python311\python.exe C:\Users\me\Documents\PythonScripts\main.py "%V"which works with no issues.
import winreg
menu_name = "My Python Script"
icon_path = "%SystemRoot%\\system32\\imageres.dll,3"
python = "C:\\Users\\me\\AppData\\Local\\Programs\\Python\\Python311\\python.exe"
command = f"{python} C:\\Users\\me\\Documents\\PythonScripts\\main.py \"%V\""
key = winreg.OpenKey(winreg.HKEY_CLASSES_ROOT,
r"Directory\Background\shell",
0, winreg.KEY_WRITE)
menu_key = winreg.CreateKey(key, menu_name)
winreg.SetValueEx(menu_key, "Icon", 0, winreg.REG_SZ, icon_path)
winreg.SetValueEx(menu_key, "command", 0, winreg.REG_SZ, command)
winreg.CloseKey(menu_key)
winreg.CloseKey(key)
I found where the issue resided. I was adding the command key incorrectly. I needed to use command_key = winreg.CreateKey(menu_key, "command") for the command. Then add the value winreg.SetValueEx(command_key, "", 0, winreg.REG_SZ, command).
import winreg
# Name that will appear in the context menu
menu_name = "My Python Script"
# Path to the icon file
icon_path = "%SystemRoot%\\system32\\imageres.dll,3"
python = "C:\\Users\\me\\AppData\\Local\\Programs\\Python\\Python311\\python.exe"
# Path to the Python script and any command-line arguments
command = f"{python} C:\\Users\\me\\Documents\\PythonScripts\\main.py \"%V\""
# Open the registry key
key = winreg.OpenKey(winreg.HKEY_CLASSES_ROOT,
r"Directory\Background\shell",
0, winreg.KEY_WRITE)
# Create a new key for our menu item
menu_key = winreg.CreateKey(key, menu_name)
command_key = winreg.CreateKey(menu_key, "command")
# Set the menu item's icon
winreg.SetValueEx(menu_key, "Icon", 0, winreg.REG_SZ, icon_path)
# Create a new key for our menu item
winreg.SetValueEx(command_key, "", 0, winreg.REG_SZ, command)
# Close the keys
winreg.CloseKey(menu_key)
winreg.CloseKey(command_key)
winreg.CloseKey(key)

Python 3 script how do i add a shortcut to recently added in the windows 10 start menu?

I am currently running python 3.8. While I am able to add a shortcut to the script to the windows 10 start menu it does not show in recently added.
I have also tried winshell.recent
import os, winshell
from win32com.client import Dispatch
Start_menu = winshell.start_menu()
path = os.path.join(Start_menu, "A test.lnk")
target = r"Python Project\test2.pyw"
wDir = r"Python Project"
icon = r"C:\\Users\\"+str(os.getlogin())+"\\Desktop\\Python Project\\icon.ico"
shell = Dispatch('WScript.Shell')
shortcut = shell.CreateShortCut(path)
shortcut.Targetpath = target
shortcut.WorkingDirectory = wDir
shortcut.IconLocation = icon
shortcut.save()

Login to a website then open it in browser

I am trying to write a Python 3 code that logins in to a website and then opens it in a web browser to be able to take a screenshot of it.
Looking online I found that I could do webbrowser.open('example.com')
This opens the website, but cannot login.
Then I found that it is possible to login to a website using the request library, or urllib.
But the problem with both it that they do not seem to provide the option of opening a web page.
So how is it possible to login to a web page then display it, so that a screenshot of that page could be taken
Thanks
Have you considered Selenium? It drives a browser natively as a user would, and its Python client is pretty easy to use.
Here is one of my latest works with Selenium. It is a script to scrape multiple pages from a certain website and save their data into a csv file:
import os
import time
import csv
from selenium import webdriver
cols = [
'ies', 'campus', 'curso', 'grau_turno', 'modalidade',
'classificacao', 'nome', 'inscricao', 'nota'
]
codigos = [
96518, 96519, 96520, 96521, 96522, 96523, 96524, 96525, 96527, 96528
]
if not os.path.exists('arquivos_csv'):
os.makedirs('arquivos_csv')
options = webdriver.ChromeOptions()
prefs = {
'profile.default_content_setting_values.automatic_downloads': 1,
'profile.managed_default_content_settings.images': 2
}
options.add_experimental_option('prefs', prefs)
# Here you choose a webdriver ("the browser")
browser = webdriver.Chrome('chromedriver', chrome_options=options)
for codigo in codigos:
time.sleep(0.1)
# Here is where I set the URL
browser.get(f'http://www.sisu.mec.gov.br/selecionados?co_oferta={codigo}')
with open(f'arquivos_csv/sisu_resultados_usp_final.csv', 'a') as file:
dw = csv.DictWriter(file, fieldnames=cols, lineterminator='\n')
dw.writeheader()
ies = browser.find_element_by_xpath('//div[#class ="nome_ies_p"]').text.strip()
campus = browser.find_element_by_xpath('//div[#class ="nome_campus_p"]').text.strip()
curso = browser.find_element_by_xpath('//div[#class ="nome_curso_p"]').text.strip()
grau_turno = browser.find_element_by_xpath('//div[#class = "grau_turno_p"]').text.strip()
tabelas = browser.find_elements_by_xpath('//table[#class = "resultado_selecionados"]')
for t in tabelas:
modalidade = t.find_element_by_xpath('tbody//tr//th[#colspan = "4"]').text.strip()
aprovados = t.find_elements_by_xpath('tbody//tr')
for a in aprovados[2:]:
linha = a.find_elements_by_class_name('no_candidato')
classificacao = linha[0].text.strip()
nome = linha[1].text.strip()
inscricao = linha[2].text.strip()
nota = linha[3].text.strip().replace(',', '.')
dw.writerow({
'ies': ies, 'campus': campus, 'curso': curso,
'grau_turno': grau_turno, 'modalidade': modalidade,
'classificacao': classificacao, 'nome': nome,
'inscricao': inscricao, 'nota': nota
})
browser.quit()
In short, you set preferences, choose a webdriver (I recommend Chrome), point to the URL and that's it. The browser is automatically opened and start executing your instructions.
I have tested using it to log in and it works fine, but never tried to take screenshot. It theoretically should do.

Pyqt4 MDI no clean mdiarea

I am new to PyQt, I am developing an application like MDI, I use the QDesigner to build the forms (I come from Visual Basic), so far so good, but it is costing me horrors to run the application well, when I close a window, erase everything, but the frame remains on the mdi-area, click on the x and close. I attach the code.
With this I knock at the window:
def ordentrabajo(self):
self.cot = FichaOrdenTrabajo()
subwindow = QtGui.QMdiSubWindow()
self.cot.mdiarea = self.ventana.mdiArea
subwindow.setWidget(self.cot)
subwindow.setGeometry(0, 0, 595, 490)
self.ventana.mdiArea.addSubWindow(subwindow)
subwindow.showventana = self.cot.show()
And within the FichaOrdenTrabajo I close with:
def closeEvent(self, QCloseEvent):
self.emit(QtCore.SIGNAL('cerrar'))
general.saveposic('ctrlordentrabajo', self.x(), self.y())
self.subwindow.close = self.cdeta.close()
self.cdeta.hide()
# del self.subwindow
self.subwindow = None
leaving the main window in this state:
The question is how to do so that when closing the mdi-area is clean?
Thank you

pyside qdialog box or qaction icon is closing app

The issue is a combination of setting an icon on the action in the toolbar (or qpushbutton) and showing a qmessagebox when triggered. If I remove the icon, the message box displays. And if I remove the message box but keep the icon, the app stays open. The other odd thing is, if I add the icon to the push button but not the action, and click on the action, it still closes the app. The doubly odd thing is if I add main.qpush_button_clicked before qapplication.exec_(), the message box is displayed. However, the next time I click on either, it closes the app.
I have looked at multiple posts and some of the ideas were to use setQuitOnLastWindowClosed, but that did not fix the issue. I also implemented event to see what was happening. When I click on either of the items, it triggers a ChildAdded event, then closes.
Also, this only does not work when I use cx_Freeze on a Mac. I have not tried on Win. It works properly when run using Eclipse or from CLI.
Does anyone have any ideas on what might be causing this, or how to fix it besides not using icons.
The icon I used is from Android icon pack.
I can add the crash log if you need it.
class Main(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.qicon = QIcon('../ic_add_black_24dp_1x.png')
self.tool_bar = self.addToolBar('main')
qaction = QAction(self)
self.tool_bar.addAction(qaction)
qaction.setText('add')
qaction.setIcon(self.qicon)
qaction.triggered.connect(self.qpush_button_clicked)
qpush_button = QPushButton('add')
self.setCentralWidget(qpush_button)
qpush_button.setIcon(self.qicon)
qpush_button.clicked.connect(self.qpush_button_clicked)
def qpush_button_clicked(self, *args, **kwargs):
QMessageBox.critical(self, 'test', 'testing')
if __name__ == '__main__':
qapplication = QApplication(sys.argv)
main = Main()
main.show()
main.raise_()
qapplication.exec_()
And here is the setup file
name = 'dialog'
version = '0.1'
description = 'description'
packages = ('os',)
excludes = ('tkinter',)
include_files = ('ic_add_black_24dp_1x.png',)
build_exe = dict(packages=packages,
excludes=excludes,
include_files=include_files)
options = dict(build_exe=build_exe)
base = 'Win32GUI' if sys.platform == 'win32' else None
script = 'dialog.py'
executables = (Executable(script, base=base),)
setup(name=name,
version=version,
description=description,
options=options,
executables=executables)
PySide Version : 1.2.2
PySide Component: : (1, 2, 2, 'final', 0)
PySide Compiled Qt : 4.8.7
PySide Qt Component: (4, 8, 7)
Running Qt Version : 4.8.7

Resources