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)
Related
I am creating a GUI in tkinter having a listbox and a Text item in a child window which appears after a button click. Listbox is displaying values of a dict which are basically names of files/directories in disk image.
I want to change the text of Text widget on <ListboxSelect> event and display type or path of selected file.
Now I cant make Text global since it has to appear on child window, so I need a way to access it in event handler of Listbox. Can I give handler reference of Textbox?
Here is my code;
def command(event):
... #Need to change the Text here, how to access it?
def display_info(dict,filename):
child_w = Tk()
listbox = Listbox(child_w)
textview = Text(child_w)
...
listbox.bind(<ListboxSelect>,command)
def upload_file():
window = Tk()
upl_button = Button(command=upload_file)
window.mainloop()
Is there a way to create a textview as global and then change its properties later to be displayed in child_window etc.
Two solutions here that I can think of is to make textview a globalized variable or to pass textview to command() as an argument.
Parameter solution:
def command(event,txtbox):
txtbox.delete(...)
def display_info(dict,filename):
child_w = Tk()
listbox = Listbox(child_w)
textview = Text(child_w)
...
listbox.bind('<ListboxSelect>',lambda event: command(event,textview))
Or simply just globalize it:
def command(event):
textview.delete(...)
def display_info(dict,filename):
global textview
child_w = Tk()
listbox = Listbox(child_w)
textview = Text(child_w)
...
listbox.bind('<ListboxSelect>',command)
While saying all this, it is good to keep in mind that creating more than one instance of Tk is almost never a good idea. Read: Why are multiple instances of Tk discouraged?
I want to be able to dynamically change the window title based on the data being displayed in the window. I display a window with a selected weather alert. In the window title, I want to put the alert event name. Each selected alert will have its own event name.
I created a small test to show using a variable in the title works.
from tkinter import *
root = Tk()
mytitle='MYTITLE'
root.title(mytitle)
root.mainloop()
However when I apply what I think is the same setup in my popup window I get title "PY_VAR0".
def msgwindow(x):
print('in message window')
alert_info = Toplevel(root) # Child window
alert_info.geometry("600x700") # Size of the window
alert_row = alert_tree.item(alert_tree.focus()) # selected value to display
alert_row_title = tk.StringVar()
alert_row_title.set(alert_row['values'][1])
print('value1:', alert_row['values'][1], 'title:', alert_row_title)
alert_info.title(alert_row_title)
alert_row_value = tk.StringVar()
alert_row_value.set(alert_row['values'][4])
alert_label = tk.Label(alert_info, textvariable=alert_row_value, justify=LEFT)
alert_label.grid(row=1, column=0, sticky=N+W)
I added a 'print' which shows my title variable has the intended value but the same variable used in title shows PY_VAR0
value1: High Surf Advisory title: PY_VAR0
The the data in alert_row_value displays as intended. This must be simple...what am I doing wrong?
alert_info.title() requires a str, not a StringVar. So, you need to use alert_row_title.get() to get a str that you can pass in. So, your code just needs to be:
alert_info.title(alert_row_title.get())
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.
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
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.