QSystemTrayIcon makes the application crash. I dont get an error in the console. Windows says AppHangB1. However, sometimes it works a few times, sometimes it crashes when first showing the context menu.
Edit:
This is my code broke down to the most important things:
class SystemTrayIcon(QSystemTrayIcon):
def __init__(self, icon, parent=None):
QSystemTrayIcon.__init__(self, icon, parent)
menu = QMenu()
showAction = menu.addAction("Fenster anzeigen")
exitAction = menu.addAction("Beenden")
menu.setStyleSheet("QMenu{background-color:white; margin:2px; "
"font: 75 10pt Trebuchet MS;} "
"QMenu::item:selected{background-color:#8e0000;}")
self.setContextMenu(menu)
self.setToolTip("KOSE")
exitAction.triggered.connect(self.exit)
showAction.triggered.connect(self.show_window)
def exit(self):
sys.exit()
def show_window(self):
if (loading_screen_window.windowOpacity() == 0.0):
loading_screen_window.fadeIN()
class CancelWindow(QWidget):
def __init__(self):
super(CancelWindow, self).__init__()
loader = QUiLoader()
file = QFile("cancel.ui")
file.open(QFile.ReadOnly)
global cancel_screen
cancel_screen = loader.load(file, self)
file.close()
self.initUI()
def initUI(self):
#some init stuff like window pos, button- function connects...
def fadeIN(self):
#window fade in animation
def fadeOUT(self, exit):
#fade out animation
def ja(self):
#button func
self.fadeOUT(True)
def nein(self):
#button func
self.fadeOUT(False)
kunden_screen.lbl_opacity.hide()
kunden_screen.setEnabled(True)
def exit_app(self):
QCoreApplication.exit()
class LoadingScreen(QWidget):
def __init__(self):
super(LoadingScreen, self).__init__()
loader = QUiLoader()
file = QFile("loading_screen.ui")
file.open(QFile.ReadOnly)
global loading_screen
loading_screen = loader.load(file, self)
file.close()
self.initUI()
#Config Thread
self.config_thread = config()
self.config_thread.finished.connect(self.config_finished)
#Config Thread starten
self.config_thread.start()
def initUI(self):
#window init
def fadeIN(self):
#fade animation
def fadeOUT(self):
#fade animation
def config_finished(self):
#function thats called when config thread is finished
class KundenSelect(QWidget):
connected = 0
cursor = None
conn_database = None
def __init__(self):
super(KundenSelect, self).__init__()
loader = QUiLoader()
file = QFile("kunden.ui")
file.open(QFile.ReadOnly)
global kunden_screen
kunden_screen = loader.load(file, self)
file.close()
self.initUI()
def initUI(self):
#window init
class config(QThread):
def run(self):
#config thread, reading files, database connection ...
app = QApplication(sys.argv)
global loading_screen_window
loading_screen_window = LoadingScreen()
global kunden_screen_window
kunden_screen_window = KundenSelect()
kunden_screen.lbl_opacity.hide()
kunden_screen_window.hide()
global cancel_screen_window
cancel_screen_window = CancelWindow()
cancel_screen_window.hide()
trayIcon = SystemTrayIcon(QIcon("icon.png"), parent=app)
trayIcon.show()
sys.exit(app.exec_())
I've googled it, but didnt find any solution. Im using PySide2 and Qt5.
Thanks
This code seems to be working just changed the signal/slot syntax
class SystemTrayIcon(QSystemTrayIcon):
def __init__(self, icon, parent=None):
QSystemTrayIcon.__init__(self, icon, parent)
menu = QMenu()
showAction = menu.addAction("Fenster anzeigen")
exitAction = menu.addAction("Beenden")
menu.setStyleSheet("QMenu{background-color:white; margin:2px; "
"font: 75 10pt Trebuchet MS;} "
"QMenu::item:selected{background-color:#8e0000;}")
self.setContextMenu(menu)
self.setToolTip("KOSE")
exitAction.triggered.connect(self.exit)
showAction.triggered.connect(self.show_window)
def exit(self):
sys.exit()
def show_window(self):
print("xxxxxx")
# if (loading_screen_window.windowOpacity() == 0.0):
# loading_screen_window.fadeIN()
app = QApplication(sys.argv)
trayIcon = SystemTrayIcon(QIcon("icon.png"), parent=app)
trayIcon.show()
sys.exit(app.exec_())
Using Pyside (not Pyside2), but I don't think it would be that different here.
I have a class to create a PyQt4 widget and another class to parse xml to get inputs. I want to create an UI dyanamically add buttons to the widget, reading from the xml(s) passed, which is not happening:
import sys
import xml.etree.ElementTree as ET
from PyQt4 import QtGui
ui = None
class userInterface(QtGui.QWidget):
def __init__(self):
super(userInterface, self).__init__()
def getFilesWindow(self):
self.filesSelected = []
fDialog = QtGui.QFileDialog.getOpenFileNames(self, 'Open file', '/Learning/Python/substance_XML_reading/', "SBS (*.sbs)")
for path in fDialog:
if path:self.filesSelected.append(path)
return self.filesSelected
class ParseXML():
def getXMLTags(self,fileList):
self.tags = []
if fileList:
print fileList
for eachFile in fileList:
fileToParse = ET.parse(eachFile)
root = fileToParse.getroot()
for child in root:
self.tags.append(child.tag)
return self.tags
def getSetUI(flist):
global ui
if flist:
tagsForBtns = ParseXML().getXMLTags(flist)
print tagsForBtns
for eachitem in tagsForBtns:
btn = QtGui.QPushButton(ui,eachitem)
def main():
app = QtGui.QApplication(sys.argv)
ui = userInterface()
fileListForUIGen = ui.getFilesWindow() # will return a list of files
getSetUI(fileListForUIGen) # Parses each file, gets tags, creates buttons and has to add to the current window..NOT WORKING
ui.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
In order to add buttons to a widget, you need to place them inside a QLayout
class Widget(QtGui.QWidget):
def __init__(self):
super(Widget, self).__init__()
self.ui_lay = QtGui.QVBoxLayout()
self.setLayout(self.ui_lay)
def addButton(self, text):
btn = QtGui.QPushButton(text, self)
self.ui_lay.addWidget(btn)
...
for eachitem in tagsForBtns:
ui.addButton(eachitem)
Also, make sure to use global ui in your main() function if you're going to be doing it this way.
Personally, I don't see the reasons for splitting up all the function calls. I would just put them all in the UserInterface class.
While building a simple GUI video downloader in python and with Glade3 I am stuck with pasting into a text entry box using an icon.
The application, and paste icon.
The python code:
# !/usr/python
import sys
import os
import subprocess as sp
try:
from gi.repository import Gtk, Gdk
except:
print('Gtk not available')
sys.exit(1)
try:
import pyGtk
pyGtk.require('2.0')
except:
pass
class VideoDownloader:
def on_mainwindow_destroy(self, object, data=None):
print "quit with cancel"
Gtk.main_quit()
def __init__(self):
self.gladefile = "lib/videodownloder.glade"
self.builder = Gtk.Builder()
self.builder.add_from_file(self.gladefile)
self.builder.connect_signals(self)
self.go = self.builder.get_object
self.window = self.go("mainwindow")
self.window.show()
self.okbtn = self.go("okbutton")
self.cancelbtn = self.go("cancelbutton")
#self.restartswitch = self.go("restartswitch")
self.contswitch = self.go("contswitch")
self.vlcswitch = self.go("vlcswitch")
self.urlentry = self.go("urlentry")
self.filechooser = self.go("filechooser")
self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
def on_urlentry_activate(self, widget):
url = self.urlentry.get_text()
print("the url is:" + url)
def on_urlentry_icon_press(self):
text = self.clipboard.set_text(self.urlentry.get_text(), -1)
print("the urlentry paste icon was clicked | 'text' ")
def on_urlentry_icon_press(self):
text = self.clipboard.wait_for_text()
print("the urlentry paste icon was clicked | 'text' ")
if text != None:
self.urlentry.set_text(text)
print(" 'text' has been pasted to the urlentry")
else:
print("No text on the clipboard")
def on_filechooser_file_activated(self, widget):
myfile = self.filechooser.get_uri()
print("the file is: " + myfile)
def on_vlcswitch_activate(self, widget):
print("VLC Switch has been activated")
def on_contswitch_activate(self, widget):
print("Continue switch has been acivated")
def on_quitbutton_clicked(self, button):
print("quit with the close button")
Gtk.main_quit()
def on_okbutton_clicked(self, button):
myfile = self.filechooser.get_uri()
url = self.urlentry.get_text()
wgetcmd = ("wget -O 'myfile ' 'url' ")
print("ok button has been clicked")
print("wget will now be run with your args: " +myfile+url)
os.system(wgetcmd)
if __name__ == "__main__":
print("videodownloader is running")
notify = os.system("notify-send --expire-time=5000 --icon='img/vid-down-logo.png' --app-name=VideoDownloader 'VideoDownloader' 'The VideoDownloader app is now running and ready!'")
notify
main = VideoDownloader()
Gtk.main()
print("videodownloader has stopped running")
When I run the code it mostly works but when I click the paste icon I get an error:
TypeError: on_urlentry_icon_press() takes exactly 1 argument (4 given)
I am fairly new to python and glade so I am probably making an elementary mistake but I do not know what is wrong and what the error means. I have searched but found only advice that didn't help.
Such as this..
Any suggestions on how to fix this please?
Your current on_urlentry_icon_press method only takes the self argument but it should take more than one because it is connected to the icon-press signal.
In the documentation you can see that it takes:
entry (a reference to the Gtk.Entry)
position (whether it is the primary or secondary icon)
event (e.g. whether it was a double click)
So it should look like this:
def on_urlentry_icon_press(self, entry, position, event):
print('urlentry icon clicked')
I'm making a custom browser in python but my AJAX calls don't work. (work on firefox and others browsers.)
#!/usr/bin/env python
import sys
import gtk
import webkit
DEFAULT_URL = '/var/www/profile.html' # Change this as you Wish
class SimpleBrowser: # needs GTK, Python, Webkit-GTK
def __init__(self):
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_position(gtk.WIN_POS_CENTER_ALWAYS)
self.window.connect('delete_event', self.close_application)
self.window.set_default_size(1366, 743)
self.window.set_position(0)
vbox = gtk.VBox(spacing=0)
vbox.set_border_width(0)
self.txt_url = gtk.Entry()
self.txt_url.connect('activate', self._txt_url_activate)
self.scrolled_window = gtk.ScrolledWindow()
self.webview = webkit.WebView()
self.scrolled_window.add(self.webview)
vbox.pack_start(self.scrolled_window, fill=True, expand=True)
self.window.add(vbox)
def _txt_url_activate(self, entry):
self._load(entry.get_text())
def _load(self, url):
self.webview.open(url)
def open(self, url):
self.txt_url.set_text(url)
self.window.set_title('007WallPaper')
self._load(url)
def show(self):
self.window.show_all()
#self.window.fullscreen()
def close_application(self, widget, event, data=None):
gtk.main_quit()
if __name__ == '__main__':
if len(sys.argv) > 1:
url = sys.argv[1]
else:
url = DEFAULT_URL
gtk.gdk.threads_init()
browser = SimpleBrowser()
browser.open(url)
browser.show()
gtk.main()
How can I "import" AJAX functionality into it ??
Maybe i can use pyqt ? Not sure how i can do that but i really need it.
I'd just need a quick example on how to easily put an icon with python on my systray. This means: I run the program, no window shows up, just a tray icon (I've got a png file) shows up in the systray and when I right-click on it a menu appears with some options (and when I click on an option, a function is run).
Is that possible? I don't need any window at all...
Examples / code snippets are REALLY appreciated! :D
For Windows & Gnome
Here ya go! wxPython is the bomb. Adapted from the source of my Feed Notifier application.
import wx
TRAY_TOOLTIP = 'System Tray Demo'
TRAY_ICON = 'icon.png'
def create_menu_item(menu, label, func):
item = wx.MenuItem(menu, -1, label)
menu.Bind(wx.EVT_MENU, func, id=item.GetId())
menu.AppendItem(item)
return item
class TaskBarIcon(wx.TaskBarIcon):
def __init__(self):
super(TaskBarIcon, self).__init__()
self.set_icon(TRAY_ICON)
self.Bind(wx.EVT_TASKBAR_LEFT_DOWN, self.on_left_down)
def CreatePopupMenu(self):
menu = wx.Menu()
create_menu_item(menu, 'Say Hello', self.on_hello)
menu.AppendSeparator()
create_menu_item(menu, 'Exit', self.on_exit)
return menu
def set_icon(self, path):
icon = wx.IconFromBitmap(wx.Bitmap(path))
self.SetIcon(icon, TRAY_TOOLTIP)
def on_left_down(self, event):
print 'Tray icon was left-clicked.'
def on_hello(self, event):
print 'Hello, world!'
def on_exit(self, event):
wx.CallAfter(self.Destroy)
def main():
app = wx.PySimpleApp()
TaskBarIcon()
app.MainLoop()
if __name__ == '__main__':
main()
2018 version
import wx.adv
import wx
TRAY_TOOLTIP = 'Name'
TRAY_ICON = 'icon.png'
def create_menu_item(menu, label, func):
item = wx.MenuItem(menu, -1, label)
menu.Bind(wx.EVT_MENU, func, id=item.GetId())
menu.Append(item)
return item
class TaskBarIcon(wx.adv.TaskBarIcon):
def __init__(self, frame):
self.frame = frame
super(TaskBarIcon, self).__init__()
self.set_icon(TRAY_ICON)
self.Bind(wx.adv.EVT_TASKBAR_LEFT_DOWN, self.on_left_down)
def CreatePopupMenu(self):
menu = wx.Menu()
create_menu_item(menu, 'Site', self.on_hello)
menu.AppendSeparator()
create_menu_item(menu, 'Exit', self.on_exit)
return menu
def set_icon(self, path):
icon = wx.Icon(path)
self.SetIcon(icon, TRAY_TOOLTIP)
def on_left_down(self, event):
print ('Tray icon was left-clicked.')
def on_hello(self, event):
print ('Hello, world!')
def on_exit(self, event):
wx.CallAfter(self.Destroy)
self.frame.Close()
class App(wx.App):
def OnInit(self):
frame=wx.Frame(None)
self.SetTopWindow(frame)
TaskBarIcon(frame)
return True
def main():
app = App(False)
app.MainLoop()
if __name__ == '__main__':
main()
wx.PySimpleApp deprecated, here's how to use wx.App instead
Took me while to figure this out so I thought I'd share. wx.PySimpleApp is deprecated in wxPython 2.9 and beyond. Here's FogleBird's original script using wx.App instead.
import wx
TRAY_TOOLTIP = 'System Tray Demo'
TRAY_ICON = 'icon.png'
def create_menu_item(menu, label, func):
item = wx.MenuItem(menu, -1, label)
menu.Bind(wx.EVT_MENU, func, id=item.GetId())
menu.AppendItem(item)
return item
class TaskBarIcon(wx.TaskBarIcon):
def __init__(self, frame):
self.frame = frame
super(TaskBarIcon, self).__init__()
self.set_icon(TRAY_ICON)
self.Bind(wx.EVT_TASKBAR_LEFT_DOWN, self.on_left_down)
def CreatePopupMenu(self):
menu = wx.Menu()
create_menu_item(menu, 'Say Hello', self.on_hello)
menu.AppendSeparator()
create_menu_item(menu, 'Exit', self.on_exit)
return menu
def set_icon(self, path):
icon = wx.IconFromBitmap(wx.Bitmap(path))
self.SetIcon(icon, TRAY_TOOLTIP)
def on_left_down(self, event):
print 'Tray icon was left-clicked.'
def on_hello(self, event):
print 'Hello, world!'
def on_exit(self, event):
wx.CallAfter(self.Destroy)
self.frame.Close()
class App(wx.App):
def OnInit(self):
frame=wx.Frame(None)
self.SetTopWindow(frame)
TaskBarIcon(frame)
return True
def main():
app = App(False)
app.MainLoop()
if __name__ == '__main__':
main()
If you can guarantee windows and you do not want to introduce the heavy dependencies of wx, you can do this with the pywin32 extensions.
Also see this question.
For Ubuntu
class TrayIcon:
def init():
iconPath = {"Windows":os.path.expandvars("%PROGRAMFILES%/MyProgram/icon.png"),
"Linux":"/usr/share/icons/myprogramicon.png"}
if platform.system()=="Linux":
import gtk
import appindicator # Ubuntu apt-get install python-appindicator
# Create an application indicator
try:
gtk.gdk.threads_init()
gtk.threads_enter()
icon = iconPath[platform.system()]
indicator = appindicator.Indicator("example-simple-client", "indicator-messages", appindicator.CATEGORY_APPLICATION_STATUS)
indicator.set_icon(icon)
indicator.set_status (appindicator.STATUS_ACTIVE)
indicator.set_attention_icon ("indicator-messages-new")
menu = gtk.Menu()
menuTitle = "Quit"
menu_items = gtk.MenuItem(menuTitle)
menu.append(menu_items)
menu_items.connect("activate", TrayIcon.QuitApp, menuTitle)
menu_items.show()
menuTitle = "About My Program"
menu_items = gtk.MenuItem(menuTitle)
menu.append(menu_items)
menu_items.connect("activate", TrayIcon.AboutApp, menuTitle)
menu_items.show()
indicator.set_menu(menu)
except:
pass
# Run the app indicator on the main thread.
try:
t = threading.Thread(target=gtk.main)
t.daemon = True # this means it'll die when the program dies.
t.start()
#gtk.main()
except:
pass
finally:
gtk.threads_leave()
#staticmethod
def AboutApp(a1,a2):
gtk.threads_enter()
dialog = gtk.Dialog("About",
None,
gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
(gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
label = gtk.Label("My Program v0.0.1, (C)opyright ME 2015. All rights reserved.")
dialog.vbox.pack_start(label)
label.show()
label2 = gtk.Label("example.com\n\nFor more support contact me#gmail.com")
label2.show()
dialog.action_area.pack_end(label2)
response = dialog.run()
dialog.destroy()
gtk.threads_leave()
#staticmethod
def QuitApp(a1, a2):
sys.exit(0)
Cross-Platform
See PyQt: Show menu in a system tray application
There is a package called pystray (bad name, just say it out loud) but works like a charm and is more lightweight than wx or Qt. These are the links:
https://pystray.readthedocs.io/en/latest/index.html
https://pypi.org/project/pystray/
Yes. There is a cross-platform example on wiki.wxpython.org that I've tested with python 2.7 (minconda install) on macOS High Sierra (10.13.3), Windows 7, and gnome 3/centos7. It is here (ignore the page title):
https://wiki.wxpython.org/Custom%20Mac%20OsX%20Dock%20Bar%20Icon
Small mods are needed for python 3.6:
you must import wx.adv
wx.TaskBarIcon becomes wx.adv.TaskBarIcon
wx.IconFromBitmap becomes wx.Icon
Gnome 3 required installation of TopIcons Plus.
Since you don't want to have the window display (" no window shows up, just a tray icon"), simply comment out the following line (though you still want to keep the wx.Frame parent):
frame.Show(True)
And since you want to use your own .png icon, remove the WXPdemo image and embeddedimage stuff and replace
icon = self.MakeIcon(WXPdemo.GetImage())
with, for example
icon = wx.Icon('icon.png')
In my experience, this will provide a good start for adapting or extending further.
An alternative if you are trying to run a python based program in the background you can run it as a service. Check out this active state recipe its pretty useful. I believe one of the options is to convert your application to exe with py2exe or pyinstall.
http://code.activestate.com/recipes/551780/
For an example, refer to this thread -> wx question.
wxPython "classic" -> [new API] wxPython 'Phoenix' (Py3)