How to combine numerous programs into one code? - python

I have a program that opens numerous other programs.
How can I combine the code from the other programs into this code so that instead of 10 sets of code i will only have one?
Code for app that I want to put all the codes in:
import pygtk
pygtk.require('2.0')
import gtk
import subprocess
class Example:
def __init__(self):
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_size_request(600, 600)
window.set_title("GTK Menu")
window.connect("delete_event",
lambda w,e: gtk.main_quit())
# Add Vbox so that we can have numerous widgets
vbox = gtk.VBox(False, 0)
window.add(vbox)
vbox.show()
#Adding a status bar
self.statusbar = gtk.Statusbar()
vbox.pack_start(self.statusbar, False, False, 0)
#Adding 5 buttons one for each of the activities
button = gtk.Button("Write")
button.connect("clicked", self.clicked_Write)
vbox.pack_end(button, True, True, 2)
button.show()
button2 = gtk.Button("Draw")
button2.connect("clicked", self.clicked_Scrible)
vbox.pack_end(button2, True, True, 2)
button2.show()
button3 = gtk.Button("Final Test")
button3.connect("clicked", self.clicked_Final)
vbox.pack_end(button3, True, True, 2)
button3.show()
button4 = gtk.Button("Helloworld")
button4.connect("clicked", self.clicked_Hello)
vbox.pack_end(button4, True, True, 2)
button4.show()
button5 = gtk.Button("Facebook")
button5.connect("clicked", self.clicked_Facebook)
vbox.pack_end(button5, True, True, 2)
button5.show()
button6 = gtk.Button("SpinButtons")
button6.connect("clicked", self.clicked_Spin)
vbox.pack_end(button6, True, True, 2)
button6.show()
button7 = gtk.Button("Calendar")
button7.connect("clicked", self.clicked_Cal)
vbox.pack_end(button7, True, True, 2)
button7.show()
button8 = gtk.Button("Colour Wheel(click popup)")
button8.connect("clicked", self.clicked_Wheel)
vbox.pack_end(button8, True, True, 2)
button8.show()
button9 = gtk.Button("Choose File")
button9.connect("clicked", self.clicked_File)
vbox.pack_end(button9, True, True, 2)
button9.show()
button10 = gtk.Button("Word Completer")
button10.connect("clicked", self.clicked_Word)
vbox.pack_end(button10, True, True, 2)
button10.show()
window.show_all()
def clicked_Write(self, widget):
# push a new message to the statusbar, using context_id 0
self.statusbar.push(0, "You have oppened Write")
subprocess.Popen(["python", "Helloworld.py"])
def clicked_Scrible(self, widget):
self.statusbar.push(0, "You have opened the Drawind Pad")
subprocess.Popen(["python", "scrible.py"])
def clicked_Final(self, widget):
self.statusbar.push(0, "You have opened the Final Exam")
subprocess.Popen(["python", "Final.py"])
def clicked_Hello(self, widget):
self.statusbar.push(0, "You have opened Helloworld")
subprocess.Popen(["python", "Helloword.py"])
def clicked_Facebook(self, widget):
self.statusbar.push(0, "You have opened Facebook")
subprocess.Popen(["python", "facebookfinal.py"])
def clicked_Spin(self, widget):
self.statusbar.push(0, "You have opened the Spin Buttons App")
subprocess.Popen(["python", "SpinButtons.py"])
def clicked_Cal(self, widget):
self.statusbar.push(0, "You have opened the Calender")
subprocess.Popen(["python", "Calender.py"])
def clicked_Wheel(self, widget):
self.statusbar.push(0, "You have opened the Colour Wheel")
subprocess.Popen(["python", "colour.py"])
def clicked_File(self, widget):
self.statusbar.push(0, "You have opened File Chooser")
subprocess.Popen(["python", "filechooser.py"])
def clicked_Word(self, widget):
self.statusbar.push(0, "You have opened the Word Completer")
subprocess.Popen(["python", "finisher.py"])
def main(self):
gtk.main()
return 0
Example().main()
Here is the code for one of the programmes I open
Colour.py
import pygtk
pygtk.require('2.0')
import gtk
class ColorSelectionExample:
# Color changed handler
def color_changed_cb(self, widget):
# Get drawingarea colormap
colormap = self.drawingarea.get_colormap()
# Get current color
color = self.colorseldlg.colorsel.get_current_color()
# Set window background color
self.drawingarea.modify_bg(gtk.STATE_NORMAL, color)
# Drawingarea event handler
def area_event(self, widget, event):
handled = False
# Check if we've received a button pressed event
if event.type == gtk.gdk.BUTTON_PRESS:
handled = True
# Create color selection dialog
if self.colorseldlg == None:
self.colorseldlg = gtk.ColorSelectionDialog(
"Select background color")
# Get the ColorSelection widget
colorsel = self.colorseldlg.colorsel
colorsel.set_previous_color(self.color)
colorsel.set_current_color(self.color)
colorsel.set_has_palette(True)
# Connect to the "color_changed" signal
colorsel.connect("color_changed", self.color_changed_cb)
# Show the dialog
response = self.colorseldlg.run()
if response -- gtk.RESPONSE_OK:
self.color = colorsel.get_current_color()
else:
self.drawingarea.modify_bg(gtk.STATE_NORMAL, self.color)
self.colorseldlg.hide()
return handled
# Close down and exit handler
def destroy_window(self, widget, event):
gtk.main_quit()
return True
def __init__(self):
self.colorseldlg = None
# Create toplevel window, set title and policies
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_title("Color selection test")
window.set_resizable(True)
# Attach to the "delete" and "destroy" events so we can exit
window.connect("delete_event", self.destroy_window)
# Create drawingarea, set size and catch button events
self.drawingarea = gtk.DrawingArea()
self.color = self.drawingarea.get_colormap().alloc_color(0, 65535, 0)
self.drawingarea.set_size_request(200, 200)
self.drawingarea.set_events(gtk.gdk.BUTTON_PRESS_MASK)
self.drawingarea.connect("event", self.area_event)
# Add drawingarea to window, then show them both
window.add(self.drawingarea)
self.drawingarea.show()
window.show()
def main():
gtk.main()
return 0
if __name__ == "__main__":
ColorSelectionExample()
main()

Put the files in the same directory, then use something like:
from Colour import ColorSelectionExample
then to run it, construct a ColorSelectionExample:
ColorSelectionExample()

Related

GtkDrawingArea gets messed up when resized

I'm working on a GTK+ frontend for libvlc in python using python-vlc. So far I followed this guide, which is working fine, except that when I resize the window, the DrawingArea gets messed up as you can see in the picture (there's probably a word for that phenomenon I don't know).
I'm getting these warnings in the console, but am not sure if this is related:
[00007fce1c014eb0] main filter error: Failed to create video converter
[00007fce2807ff70] vdpau_avcodec generic error: Xlib is required for VDPAU
I already tried setting the background color of the window using the css styling for GTK+ but it had no effect.
I think this should not happen, am I missing something? I'm on wayland by the way.
Using Google and checking different examples I created code which fills background in DrawingArea with black color.
Assign drawing function to DrawingArea
self.draw_area = Gtk.DrawingArea()
self.draw_area.connect("draw", self.da_draw_event)
Function which fills area
def da_draw_event(self, widget, cairo_ctx):
cairo_ctx.set_source_rgb(0, 0, 0)
cairo_ctx.paint()
Full code
import sys
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
gi.require_version('GdkX11', '3.0')
from gi.repository import GdkX11
import vlc
MRL = ""
class ApplicationWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Python-Vlc Media Player")
self.player_paused=False
self.is_player_active = False
self.connect("destroy", Gtk.main_quit)
def show(self):
self.show_all()
def setup_objects_and_events(self):
self.playback_button = Gtk.Button()
self.stop_button = Gtk.Button()
self.play_image = Gtk.Image.new_from_icon_name(
"gtk-media-play",
Gtk.IconSize.MENU
)
self.pause_image = Gtk.Image.new_from_icon_name(
"gtk-media-pause",
Gtk.IconSize.MENU
)
self.stop_image = Gtk.Image.new_from_icon_name(
"gtk-media-stop",
Gtk.IconSize.MENU
)
self.playback_button.set_image(self.play_image)
self.stop_button.set_image(self.stop_image)
self.playback_button.connect("clicked", self.toggle_player_playback)
self.stop_button.connect("clicked", self.stop_player)
self.draw_area = Gtk.DrawingArea()
self.draw_area.set_size_request(300, 300)
self.draw_area.connect("realize",self._realized)
self.draw_area.connect("draw", self.da_draw_event)
self.hbox = Gtk.Box(spacing=6)
self.hbox.pack_start(self.playback_button, True, True, 0)
self.hbox.pack_start(self.stop_button, True, True, 0)
self.vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
self.add(self.vbox)
self.vbox.pack_start(self.draw_area, True, True, 0)
self.vbox.pack_start(self.hbox, False, False, 0)
def da_draw_event(self, widget, cairo_ctx):
#print('da_draw_event')
#print('widget:', widget)
#print('cairo_ctx:', cairo_ctx)
cairo_ctx.set_source_rgb(0, 0, 0)
cairo_ctx.paint()
def stop_player(self, widget, data=None):
self.player.stop()
self.is_player_active = False
self.playback_button.set_image(self.play_image)
def toggle_player_playback(self, widget, data=None):
"""
Handler for Player's Playback Button (Play/Pause).
"""
if self.is_player_active == False and self.player_paused == False:
self.player.play()
self.playback_button.set_image(self.pause_image)
self.is_player_active = True
elif self.is_player_active == True and self.player_paused == True:
self.player.play()
self.playback_button.set_image(self.pause_image)
self.player_paused = False
elif self.is_player_active == True and self.player_paused == False:
self.player.pause()
self.playback_button.set_image(self.play_image)
self.player_paused = True
else:
pass
def _realized(self, widget, data=None):
self.vlcInstance = vlc.Instance("--no-xlib")
self.player = self.vlcInstance.media_player_new()
win_id = widget.get_window().get_xid()
self.player.set_xwindow(win_id)
self.player.set_mrl(MRL)
self.player.play()
self.playback_button.set_image(self.pause_image)
self.is_player_active = True
if __name__ == '__main__':
if not sys.argv[1:]:
print("Exiting \nMust provide the MRL.")
sys.exit(1)
if len(sys.argv[1:]) == 1:
MRL = sys.argv[1]
window = ApplicationWindow()
window.setup_objects_and_events()
window.show()
Gtk.main()
window.player.stop()
window.vlcInstance.release()
As #furas pointed out in the comments, constantly drawing a black rectangle on the DrawingArea works very well. I use the following code inside a widget that derives from Gtk.DrawingArea:
def draw(self, c, *args):
rect = self.get_allocation()
win = self.get_window().get_position()
c.set_source_rgb(0, 0, 0)
c.rectangle(rect.x - win.x, rect.y - win.y, rect.width, rect.height)
c.fill()
self.connect("draw", draw)
Edit: See #furas answer for an even simpler approach.

PythonGtk : expand NoteBook to fill the rest of my window

I am trying to resize my window using Python Gtk3, but I need to expand the Notebook object to fill the main window.
How could I make the notebook fill the rest of the window?
Like this:
Here is my code :
#!/usr/bin/python
# coding=utf-8
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
class SearchDialog(Gtk.Dialog):
def __init__(self, parent):
Gtk.Dialog.__init__(self, "Something", parent,
Gtk.DialogFlags.MODAL, buttons=(
Gtk.STOCK_NEW, Gtk.ResponseType.OK,
Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL))
self.set_default_size(400, 600)
box = self.get_content_area()
label = Gtk.Label("Insert text you want to search for:")
box.add(label)
# self.entry = Gtk.Entry()
# box.add(self.entry)
self.main_area = Gtk.Stack()
self.main_area.set_transition_type(Gtk.StackTransitionType.SLIDE_LEFT_RIGHT)
self.main_area.set_transition_duration(1000)
self.entry = Gtk.Entry()
self.main_area.add_titled(self.entry, "entry_name", "Entry Url")
self.labelS = Gtk.Label()
self.label_txt = """<big><i> label here</i></big>"""
self.labelS.set_markup(self.label_txt)
self.labelS.set_line_wrap(True)
self.main_area.add_titled(self.labelS, "label_name", "How Scan will Start")
self.our_stackSwitcher = Gtk.StackSwitcher()
self.our_stackSwitcher.set_stack(self.main_area)
box.add(self.our_stackSwitcher)
box.add(self.main_area)
self.show_all()
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Desktop app title")
self.set_default_size(1000, 648)
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
self.add(vbox)
Hbox1 = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
vbox.add(Hbox1)
Hbox2 = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
vbox.add(Hbox2)
Hbox3 = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
vbox.add(Hbox3)
Hbox4 = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
vbox.add(Hbox4)
main_menu_bar = Gtk.MenuBar()
#################################################################################
#drop down the menu
file_menu1 = Gtk.Menu()
file_menu1_dropdown = Gtk.MenuItem("File")
#File menu Items
file_new = Gtk.MenuItem("New Scan")
file_save = Gtk.MenuItem("Save History")
file_exit = Gtk.MenuItem("Exit")
file_menu1_dropdown.set_submenu(file_menu1)
file_menu1.append(file_new)
file_new.connect("activate", self.Onclick_new)
file_menu1.append(file_save)
file_save.connect("activate", self.Onclick_save)
file_menu1.append(Gtk.SeparatorMenuItem())
file_menu1.append(file_exit)
file_exit.connect("activate",self.Onclick_exit)
#add the menu to the main menu bar
main_menu_bar.append(file_menu1_dropdown)
###################################################################################
#drop down the menu
file_menu2 = Gtk.Menu()
file_menu2_dropdown = Gtk.MenuItem("Scan")
#File menu Items
file_edit = Gtk.MenuItem("Edit")
file_cancel = Gtk.MenuItem("Cancel")
file_menu2_dropdown.set_submenu(file_menu2)
file_menu2.append(file_edit)
file_edit.connect("activate", self.Onclick_edit)
file_menu2.append(file_cancel)
file_cancel.connect("activate", self.Onclick_cancel)
#add the menu to the main menu bar
main_menu_bar.append(file_menu2_dropdown)
###################################################################################
#drop down the menu
file_menu3 = Gtk.Menu()
file_menu3_dropdown = Gtk.MenuItem("Help")
#File menu Items
file_mode = Gtk.MenuItem("Mode")
file_about = Gtk.MenuItem("About")
file_menu3_dropdown.set_submenu(file_menu3)
file_menu3.append(file_mode)
file_mode.connect("activate", self.Onclick_mode)
file_menu3.append(file_about)
file_about.connect("activate", self.Onclick_about)
#add the menu to the main menu bar
main_menu_bar.append(file_menu3_dropdown)
###################################################################################
## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##
Hbox1.pack_start(main_menu_bar , True, True, 0)
label1_Hbox2 = Gtk.Label(" i am label 1 of Horizental box 2")
Hbox2.pack_start(label1_Hbox2 , True, True, 0)
label2_Hbox2 = Gtk.Label(" i am label 2 of Horizental box 2")
Hbox2.pack_start(label2_Hbox2, True, True, 0)
self.notebook1 = Gtk.Notebook()
Hbox3.pack_start(self.notebook1, True, True, 0)
self.notebook2 = Gtk.Notebook()
Hbox3.pack_start(self.notebook2, True, True, 0)
##################################################################################################
self.page1 = Gtk.Box()
self.page1.set_border_width(3)
self.label = Gtk.Label('label Number 1-------')
self.page1.add(self.label)
self.label2 = Gtk.Label('label Number 2 should appear in next line')
self.page1.add(self.label2)
self.notebook1.append_page(self.page1, Gtk.Label('Tab Pane 1 panel 1'))
###################################################################################################
self.page1 = Gtk.Box()
self.notebook2.append_page(self.page1, Gtk.Label('Tab Pane 1 panel 2'))
self.page2 = Gtk.Box()
self.notebook2.append_page(self.page2, Gtk.Label('Tab Pane 2 panel 2'))
def Onclick_new(self, widget):
dialog = SearchDialog(self)
response = dialog.run()
if response == Gtk.ResponseType.OK:
self.label.set_text(dialog.entry.get_text())
dialog.destroy()
def Onclick_save(self, widget):
print("Save clicked")
def Onclick_exit(self, widget):
print("exit clicked")
Gtk.main_quit()
def Onclick_edit(self, widget):
print("Edit clicked")
def Onclick_cancel(self, widget):
print("Cancel clicked")
def Onclick_mode(self, widget):
print("Mode of use clicked")
def Onclick_about(self, widget):
print("About clicked")
#cssProvider = Gtk.CssProvider()
#cssProvider.load_from_path('gtkStyledButtonTest.css')
#screen = Gdk.Screen.get_default()
#styleContext = Gtk.StyleContext()
#styleContext.add_provider_for_screen(screen, cssProvider,
# Gtk.STYLE_PROVIDER_PRIORITY_USER)
win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

How to get background of textview in pygobject “gtk3”?

I'd like to get the current background color of my textview to change it and restore it later.
here what I tried:
context = textview.get_style_context()
state = Gtk.StateFlags.NORMAL
color = context.get_background_color(state)
I tried all possible states, but none returns the correct background color (white in my case)
Any idea how to get it?
I'm not exactly sure what you specific problem is without seeing more code, but here is a quick example that overrides the background and then restores it on a button click:
from gi.repository import Gtk, Gdk
import sys
class MyWindow(Gtk.ApplicationWindow):
def __init__(self, app):
Gtk.Window.__init__(self, title="Textview example", application=app)
self.set_default_size(250, 100)
self.set_border_width(10)
self.view = Gtk.TextView()
self.style_context = self.view.get_style_context()
self.default_bg_color = self.style_context.get_background_color(Gtk.StateFlags.NORMAL)
self.view.override_background_color(Gtk.StateFlags.NORMAL,
Gdk.RGBA(0, 0, 0, 1))
self.btn = Gtk.Button(label="Click Here")
self.btn.connect("clicked", self.on_btn_clicked)
box = Gtk.VBox()
box.pack_start(self.view, True, True, 0)
box.pack_start(self.btn, False, False, 0)
self.add(box)
def on_btn_clicked(self, widget):
current_bg = self.style_context.get_background_color(Gtk.StateFlags.NORMAL)
if current_bg == self.default_bg_color:
self.view.override_background_color(Gtk.StateFlags.NORMAL,
Gdk.RGBA(0, 0, 0, 1))
else:
self.view.override_background_color(Gtk.StateFlags.NORMAL,
self.default_bg_color)
class MyApplication(Gtk.Application):
def __init__(self):
Gtk.Application.__init__(self)
def do_activate(self):
win = MyWindow(self)
win.show_all()
def do_startup(self):
Gtk.Application.do_startup(self)
app = MyApplication()
exit_status = app.run(sys.argv)
sys.exit(exit_status)

Right-Click on a PyGTK Hbox in an Expander

I've got a gtk.Expander object, containing in its label a gtk.HBox, which packed a gtk.Image and a gtk.Label.
I want to launch a Webbrowser when the HBox is right-clicked.
Here is my code:
def launchBrowser(widget, host, event):
print event
print host
if event.type == gtk.gdk.BUTTON_PRESS:
if event.button == 3:
webbrowser.open(host, 1)
print "right click"
def addServer(self, loginfo):
main_expand = gtk.Expander()
main_led = gtk.Image()
if int(loginfo["main"]) == 0:
main_led.set_from_stock(gtk.STOCK_STOP, gtk.ICON_SIZE_BUTTON)
else:
main_led.set_from_stock(gtk.STOCK_CLOSE, gtk.ICON_SIZE_BUTTON)
main_srvname = gtk.Label(loginfo["srvname"])
expand_title = gtk.HBox(False, 2)
expand_title.pack_start(main_led, False, True, 0)
expand_title.pack_start(main_srvname, True, True, 0)
main_srvname.add_events(gtk.gdk.BUTTON_PRESS_MASK)
main_srvname.connect_object('event', self.launchBrowser, loginfo["host"])
main_srvname.emit('event', gtk.gdk.Event(gtk.gdk.BUTTON_PRESS_MASK))
main_expand.set_property("label-widget", expand_title)
Problem is that when I pass my cursor on this HBox, I correctly received the event. But when I clicked nothing happen. I think it's because the click event is intercept by the Expander.
Anyhelp is welcome =)
Thanks in advance!
EDIT:
I also tried that:
main_srvname = gtk.Label(loginfo["srvname"])
eventbox = gtk.EventBox()
eventbox.add(main_srvname)
eventbox.connect_object('button-press-event', self.launchBrowser, loginfo["host"])
# Titre de l'expand box
expand_title = gtk.HBox(False, 2)
expand_title.pack_start(main_led, False, True, 0)
expand_title.pack_start(eventbox, True, True, 0)
Not working either...
EDIT2:
As requested by Jeremy, here a self-contained code, just copy-paste it you lazyboy!
import pygtk
pygtk.require('2.0')
import gtk
class MainWindow(gtk.Window):
def __init__(self):
gtk.Window.__init__(self)
self.set_default_size(300, 300)
self.addServer()
def launchBrowser(widget, host, event):
print event
if event.type == gtk.gdk.BUTTON_PRESS:
if event.button == 3:
print "click"
def addServer(self):
main_expand = gtk.Expander()
main_led = gtk.Image()
main_led.set_from_stock(gtk.STOCK_STOP, gtk.ICON_SIZE_BUTTON)
main_srvname = gtk.Label("srvname")
main_srvname.add_events(gtk.gdk.BUTTON_PRESS_MASK)
main_srvname.connect_object('button-press-event', self.launchBrowser, "host")
expand_title = gtk.HBox(False, 2)
expand_title.pack_start(main_led, False, True, 0)
expand_title.pack_start(main_srvname, True, True, 0)
main_expand.set_property("label-widget", expand_title)
self.add(main_expand)
self.show_all()
def main():
MainWindow()
gtk.main()
if __name__ == '__main__':
main()
Small change in your code.
Add "button-press-event" on Expander widget instead of Label widget
import pygtk
pygtk.require('2.0')
import gtk
class MainWindow(gtk.Window):
def __init__(self):
gtk.Window.__init__(self)
self.set_default_size(300, 300)
self.addServer()
def launchBrowser(self, widget, event, host, *args):
if event.type == gtk.gdk.BUTTON_PRESS:
if event.button == 3:
print "click"
# Normal behaviour of Expander on single click
expand = widget.get_expanded()
if not expand: widget.set_expanded(True)
else: widget.set_expanded(False)
def addServer(self):
main_expand = gtk.Expander()
main_led = gtk.Image()
main_led.set_from_stock(gtk.STOCK_STOP, gtk.ICON_SIZE_BUTTON)
main_srvname = gtk.Label("srvname")
main_expand.add_events(gtk.gdk.BUTTON_PRESS_MASK)
main_expand.connect('button-press-event', self.launchBrowser, 'host')
expand_title = gtk.HBox(False, 2)
expand_title.pack_start(main_led, False, True, 0)
expand_title.pack_start(main_srvname, True, True, 0)
main_expand.set_property("label-widget", expand_title)
self.add(main_expand)
self.show_all()
def main():
MainWindow()
gtk.main()
if __name__ == '__main__':
main()

PyGTK how to click button and open file in pop-up window

I editted the code so that the 3 buttons now show up .Can someone please tell me how to make it so that when I click the button that says Helloworld and application called Helloworld.py will pop-up in another window.Same for the other 2 buttons
!/usr/bin/env python
# menu.py
import pygtk
pygtk.require('2.0')
import gtk
class MenuExample:
def __init__(self):
# create a new window
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_size_request(200, 100)
window.set_title("GTK Menu Test")
window.connect("delete_event", lambda w,e: gtk.main_quit())
# Init the menu-widget, and remember -- never
# show() the menu widget!!
# This is the menu that holds the menu items, the one that
# will pop up when you click on the "Root Menu" in the app
menu = gtk.Menu()
# Next we make a little loop that makes three menu-entries for
# "test-menu". Notice the call to gtk_menu_append. Here we are
# adding a list of menu items to our menu. Normally, we'd also
# catch the "clicked" signal on each of the menu items and setup a
# callback for it, but it's omitted here to save space.
for i in range(3):
# Copy the names to the buf.
buf = "Test-undermenu - %d" % i
# Create a new menu-item with a name...
menu_items = gtk.MenuItem(buf)
# ...and add it to the menu.
menu.append(menu_items)
# Do something interesting when the menuitem is selected
menu_items.connect("activate", self.menuitem_response, buf)
# Show the widget
menu_items.show()
# This is the root menu, and will be the label
# displayed on the menu bar. There won't be a signal handler attached,
# as it only pops up the rest of the menu when pressed.
root_menu = gtk.MenuItem("Root Menu")
root_menu.show()
# Now we specify that we want our newly created "menu" to be the
# menu for the "root menu"
root_menu.set_submenu(menu)
# A vbox to put a menu and a button in:
vbox = gtk.VBox(False, 0)
window.add(vbox)
vbox.show()
# Create a menu-bar to hold the menus and add it to our main window
menu_bar = gtk.MenuBar()
vbox.pack_start(menu_bar, False, False, 2)
menu_bar.show()
# Create a button to which to attach menu as a popup
button = gtk.Button("HelloWorld")
button.connect_object("event", self.button_press, menu)
vbox.pack_end(button, True, True, 2)
button.show()
button2 = gtk.Button("Scrible")
button2.connect_object("event", self.button_press, menu)
vbox.pack_end(button2, True, True, 2)
button2.show()
button3 = gtk.Button("Final")
button3.connect_object("event", self.button_press, menu)
vbox.pack_end(button3, True, True, 2)
button3.show()
# And finally we append the menu-item to the menu-bar -- this is the
# "root" menu-item I have been raving about =)
menu_bar.append (root_menu)
# always display the window as the last step so it all splashes on
# the screen at once.
window.show()
# Respond to a button-press by posting a menu passed in as widget.
#
# Note that the "widget" argument is the menu being posted, NOT
# the button that was pressed.
def button_press(self, widget, event):
if event.type == gtk.gdk.BUTTON_PRESS:
widget.popup(None, None, None, event.button, event.time)
# Tell calling code that we have handled this event the buck
# stops here.
return True
# Tell calling code that we have not handled this event pass it on.
return False
def button2_press(self, widget, event):
if event.type == gtk.gdk.BUTTON2_PRESS:
widget.popup(None, None, None, event.button, event.time)
return True
return False
def button3_press(self, widget, event):
if event.type == gtk.gdk.BUTTON3_PRESS:
widget.popup(None, None, None, event.button, event.time)
return True
return False
# Print a string when a menu item is selected
def menuitem_response(self, widget, string):
print "%s" % string
def main():
gtk.main()
return 0
if __name__ == "__main__":
MenuExample()
main()
You could do something like this. I'm assuming you just want to execute your .py files, e.g. helloworld.py etc. I'm using Popen from subprocess to execute python (not assuming the py files are executable) scripts. Note that I've edited the script to only have one button, this is just to show you the idea.
import pygtk
pygtk.require('2.0')
import gtk
import subprocess
class Example:
def __init__(self):
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_size_request(200, 100)
window.set_title("GTK Menu Test")
window.connect("delete_event",
lambda w,e: gtk.main_quit())
vbox = gtk.VBox(False, 0)
window.add(vbox)
vbox.show()
button = gtk.Button("HelloWorld")
button.connect("clicked", self.clicked_helloworld)
vbox.pack_end(button, True, True, 2)
button.show()
window.show_all()
def clicked_helloworld(self, widget):
subprocess.Popen(["python", "helloworld.py"])
def main(self):
gtk.main()
return 0
Example().main()

Categories

Resources