Why is this PyGTK menu empty? - python

I am working on a PyGTK application with a Gtk.Toolbar. One of the items in taht toolbar is a Gtk.MenuToolButton. However, clicking on the arrow once the program runs results in an empty menu (a tiny white sliver). Here is the code that sets up the menu:
self.compileMenu = Gtk.Menu()
self.defaultCompileOption = Gtk.MenuItem(label = "Compile with defaults")
self.configCompileOption = Gtk.MenuItem(label = "Change compile options...")
self.compileWithFiles = Gtk.MenuItem(label = "Bundle files...")
self.compileMenu.append(self.defaultCompileOption)
self.compileMenu.append(self.configCompileOption)
self.compileMenu.append(self.compileWithFiles)
self.compileButton = Gtk.MenuToolButton(icon_name = "x-package-repository", label = "Compile...", menu = self.compileMenu)
There is also a call to self.toolbar.add at the bottom, to add the MenuToolButton to the toolbar.

Found the issue! Turns out that for some reason you need to call, in my case, compileMenu.show_all() to make it realize that you added things to it.

Related

Why can't add more than one custom button?

I try to add 3 buttons inside a Gtk.Box, this Box was created in my Window design (with Glade). See the code;
# Button for IPCamera (MJpeg stream)
self.buttonIPCam1 = Gtk.Button()
self.buttonIPCam1.add(self.imageIPCam)
self.buttonIPCam1.connect("clicked", self.triggerIPCam1)
self.buttonIPCam1.props.relief = Gtk.ReliefStyle.NONE
self.placeButtonCameras.add(self.buttonIPCam1)
self.buttonIPCam2 = Gtk.Button()
self.buttonIPCam2.add(self.imageIPCam)
self.buttonIPCam2.connect("clicked", self.triggerIPCam2)
self.buttonIPCam2.props.relief = Gtk.ReliefStyle.NONE
self.placeButtonCameras.add(self.buttonIPCam2)
self.buttonIPCam3 = Gtk.Button()
self.buttonIPCam3.add(self.imageIPCam)
self.buttonIPCam3.connect("clicked", self.triggerIPCam3)
self.buttonIPCam3.props.relief = Gtk.ReliefStyle.NONE
self.placeButtonCameras.add(self.buttonIPCam3)
self.placeButtonCameras.show_all()
Only one button with icon is displayed and got this error message;
(app_pre.py:3068): Gtk-WARNING **: 13:30:27.422: Attempting to add a widget with type GtkImage to a container of type GtkButton, but the widget is already inside a container of type GtkButton, please remove the widget from its existing container first.
I'm beginner and don't known why I can't add more than one Button with Image at time for this container "Box" named; self.placeButtonCameras.
In future, is for adding buttons from a config (0 to 5 cameras possibles)
The problem is not that you're adding multiple buttons, but that you're trying to re-use a GtkImage in multiple GtkButtons. Rather than trying to re-use the widget, you should create a new one each time.
In other words, your current code does this:
// somewhere earlier: self.imageIPCam = new Gtk.Image()
self.buttonIPCam1.add(self.imageIPCam)
// ...
self.buttonIPCam2.add(self.imageIPCam)
// ...
self.buttonIPCam3.add(self.imageIPCam)
To fix it you can do this:
imageIPCam1 = new Gtk.Image()
self.buttonIPCam1.add(self.imageIPCam1)
// ...
imageIPCam2 = new Gtk.Image()
self.buttonIPCam2.add(self.imageIPCam2)
// ...
imageIPCam3 = new Gtk.Image()
self.buttonIPCam3.add(self.imageIPCam3)

PyQT4 QTextBrowser auto-scroll

I need a little help here.
I have this QTextBrowser where I redirect all stdout to it.
self.console_window = QtGui.QTextBrowser()
self.console_window.setReadOnly(True)
What I need now is to auto scroll to the bottom so I can see what is happening without the need of manually scroll to the bottom.
I tried this
scrollBar = self.console_window.verticalScrollBar()
scrollBar.setValue(scrollBar.maximum())
but is not working.
Any thoughts?
FIXED!!!
def handleOutput(self, text, stdout):
self.console_window.moveCursor(QtGui.QTextCursor.End)
self.console_window.ensureCursorVisible()
self.console_window.insertPlainText(text)
def Console_Window(self):
self.console_window = QtGui.QTextEdit()
self.console_window.setReadOnly(True)
just a quick update in Pyqt5.
i did it bit differently, as i've seen that a delay is needed:
self.scrollbar = self.log_output.verticalScrollBar() #the self.scrollbar is the same as your self.console_window
try:
time.sleep(0.1) #needed for the refresh
self.scrollbar.setValue(10000) #try input different high value
except:
pass #when it is not available

In Tkinter how to pass a called function as argument?

In Tkinter for constructing the menubar with the <menu_item>.add_command() we need a string for the accelerator argument which will create the hotkey binding for a command.
I created a method, which is checking if the user's platform is Mac or other, and if it is, then returns the Command key string combined with the other keys.
But it doesn't work -> the menu is building, if I click on the menu-item it is working, but not working with the hot-keys. ALthough I can see the ⌘ + N in the menu..
My first thought is, that the self.hot_key() method is not called while passed as an argument..
import sys
import Tkinter
class app(object):
def __init__(self):
self.gui = Tkinter.Tk()
self.gui.minsize(width=640, height=320)
menu = Tkinter.Menu(self.gui)
filemenu = Tkinter.Menu(menu, tearoff=0)
filemenu.add_command(
label = 'New',
command = self.New,
accelerator = self.hot_key('n')
)
menu.add_cascade(
label = 'File',
menu = filemenu
)
self.gui.config(menu=menu)
self.text = Tkinter.Text(self.gui)
self.text.pack(expand=Tkinter.YES, fill=Tkinter.BOTH)
def hot_key(self, *keys):
super_key = 'Command' if sys.platform == 'darwin' else 'Control'
return '{super}+{keys}'.format(super=super_key, keys='+'.join(keys))
def New(self):
print "I'm working!"
App = app()
App.gui.mainloop()
According to this page,
The "accelerator" option is used to indicate the menu accelerator that
should be associated with this menu. This does not actually create the
accelerator, but only displays what it is next to the menu item. You
still need to create a binding for the accelerator yourself.
So your accelerator keyword argument is working as designed -- the Command-N symbol appears in your menu.
As mgilson suggests in a comment, you can use bind_all to get the keyboard combination to actually do something.
self.gui.bind_all("<Command-n>", lambda event: self.New())

How to add a option in right click menu on ubuntu

I am making an app for ubuntu 12.04.What i want to do is add an option to the menubar which appear when we right click on some select option.
To make it more clear-
In Normally when we select some text and right click there appear some option like cut copy pasteI want to add another option how can i do it.
When clicked the option would just have to execute another application and send the selected data to that applicaion.
I would be using Glade with python for development.
you should know some basics about glade and gtk first. the following is just notes:
1-On glade you can use the menu button to create menu.
2-Right click on it an dchoose "edit ..."
3-from the window you can add items.(the right part display the name and type of the menu item , the left part display the properties of the selected item, the lower part display the signals which could connected to the menu item)
4- now connect the menu item with the function which do what you want (When clicked the option would just have to execute another application and send the selected data to that applicaion.)
5- go to your code. get the menu as usual .
self.menu = self.builder.get_object("menu")
6- to popup the menu on right click on an object, you should connect that object with the function ahich execute the menu ( assuming that its name is :on_button_press ) :
def on_button_press(self, treeview, event):
if event.button == 3:
x = int(event.x)
y = int(event.y)
time = event.time
pthinfo = treeview.get_path_at_pos(x, y)
if pthinfo is not None:
path, col, cellx, celly = pthinfo
treeview.grab_focus()
treeview.set_cursor( path, col, 0)
self.popupmenu.popup( None, None, None, event.button, time)
return True

python: Right Click on list menu not showing item selected

In my ongoing effort to learn more about python, I am trying to add a right click event to my mp3 manager program. What currently works is that it shows the menu and all of the options. What is not working is the functions selected from the menu are not executing as I think they should be. Much of this code was taken from a 'how to' on another site.
Here are the right click menu options
menu_titles = ["Remove Selection from list",
"Delete Selection from system",
"Move Selection",
"Copy Selection",
"Print Selection"]
menu_title_by_id = {}
for title in menu_titles:
menu_title_by_id[ wxNewId() ] = title
The code that is run when the right click event happens
def RightClickCb( self, event ):
# record what was clicked
self.list_item_clicked = right_click_context = event.GetText()
### 2. Launcher creates wxMenu. ###
menu = wxMenu()
for (id,title) in menu_title_by_id.items():
### 3. Launcher packs menu with Append. ###
menu.Append( id, title )
### 4. Launcher registers menu handlers with EVT_MENU, on the menu. ###
EVT_MENU( menu, id, self.MenuSelectionCb )
### 5. Launcher displays menu with call to PopupMenu, invoked on the source component, passing event's GetPoint. ###
self.MainPanel.PopupMenu( menu, event.GetPoint() )
menu.Destroy() # destroy to avoid mem leak
def MenuSelectionCb( self, event ):
# do something
operation = menu_title_by_id[ event.GetId() ]
target = self.list_item_clicked
print 'Perform "%(operation)s" on "%(target)s."' % vars()
What I expect to get when I do a right-click and then select one of the options in the menu is the output
Perform "Print Selection" on "<data about the selection here>"
What I am getting is
Perform "Print Selection" on "."
How do I get the data from the item I have selected as part of my right click event?
Maybe you should use event.GetString() in place of event.GetText()
See here
Your code seems outdated tho, binding to events should be done like this:
menu.Bind(wx.EVT_MENU, self.MenuSelectionCb, id=id)
moreover if you bind all ids to the same function you can just bind once for all ids:
menu.Bind(wx.EVT_MENU, self.MenuSelectionCb)
You can find a solution under Python: Right click on objectlistview not showing item name selected where the use of objectlistview's GetSelectedObject() method is proposed instead.

Categories