Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3456650
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:45:47+00:00 2026-05-18T09:45:47+00:00

I am not that familiar with PyGTK. Please see the following code. Is it

  • 0

I am not that familiar with PyGTK. Please see the following code. Is it possible to make it do the following?

  1. There are two buttons, which we refer to as “generate” and “view”.
  2. The “generate” button will generate random values for variables A and B.
  3. The “view” button will pop up a menu showing A and B.

The problem with the following code is that the “view” menu shows A and B, but the menu does not update as the user presses the “generate” button.

I ran the code with Python 2.6.6.

Please also suggest any ways that I can improve the code (formatting, style, PyGTK conventions, …). Thank you in advance.

   """Generate values for two variables A and B."""
# ------------------------------------------------------------------------------
# Winston C. Yang
# Created 2010-12-04
# ------------------------------------------------------------------------------
# Python modules. Be alphabetical.
import random
# ------------------------------------------------------------------------------
# Other Python modules. Be alphabetical.
import gtk
import pygtk
pygtk.require("2.0")
# ------------------------------------------------------------------------------
class Generator:

    """Generate values for two variables A and B."""

    def __init__(self):

        # Create a dictionary in which a key is a variable name and a
        # (dictionary) value is the variable value.
        self.d_variable_value = {}
        # ----------------------------------------------------------------------
        window = gtk.Window()
        window.set_title("Generate")
        window.connect("destroy", self.quit_event)
        # ----------------------------------------------------------------------
        # Create a vertical box with two buttons.
        vbox = gtk.VBox()

        # Create a button to generate values for A and B.
        b = gtk.Button("Generate A and B")
        vbox.pack_start(b)
        b.connect("clicked", self.generate_variable_values)

        # Create a button to view A and B.
        b = gtk.Button("View A and B")
        vbox.pack_start(b)
        b.connect_object("event", self.button_press, self.create_menu())
        # ----------------------------------------------------------------------
        window.add(vbox)
        window.show_all()
    # --------------------------------------------------------------------------
    def quit_event(self, widget=None, event=None):
        """Quit."""
        gtk.main_quit()
    # --------------------------------------------------------------------------
    def generate_variable_values(self, widget=None):
        """Generate values for A and B."""
        self.d_variable_value = {
            "A" : random.randint(0, 10),
            "B" : random.randint(0, 10),
            }

        print "I generated " + str(self.d_variable_value)
    # --------------------------------------------------------------------------
    def button_press(self, widget, event):
        """button_press method."""
        if event.type == gtk.gdk.BUTTON_PRESS:
            widget.popup(None, None, None, event.button, event.time)
            return True

        return False
    # --------------------------------------------------------------------------
    def create_menu(self):
        """Create a menu showing A and B."""
        # How can I update the menu after the user presses the
        # "generate" button?

        # If there are no values for A and B, generate them.
        if not self.d_variable_value:
            self.generate_variable_values()

        # Show A and B in a menu.
        menu = gtk.Menu()

        for key, value in sorted(self.d_variable_value.items()):

            text = key + " " + str(value)
            item = gtk.MenuItem(text)
            item.show()
            menu.append(item)

        return menu
# ------------------------------------------------------------------------------
if __name__ == "__main__":
    Generator()
    gtk.main()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T09:45:48+00:00Added an answer on May 18, 2026 at 9:45 am

    This line b.connect_object("event", self.button_press, self.create_menu()) is connecting self.button_press to the event signal on a gtk.Menu created by self.create_menu(). This line is never executed again, so the menu is always the same.

    What I did was connect the event signal for the View A and B button to the self.button_press handler, and that handler creates an updated menu every time it’s run.

    # ------------------------------------------------------------------------------
    # Python modules. Be alphabetical.
    import random
    # ------------------------------------------------------------------------------
    # Other Python modules. Be alphabetical.
    import gtk
    import pygtk
    pygtk.require("2.0")
    # ------------------------------------------------------------------------------
    class Generator:
    
        """Generate values for two variables A and B."""
    
        def __init__(self):
    
            # Create a dictionary in which a key is a variable name and a
            # (dictionary) value is the variable value.
            self.d_variable_value = {}
            # ----------------------------------------------------------------------
            window = gtk.Window()
            window.set_title("Generate")
            window.connect("destroy", self.quit_event)
            # ----------------------------------------------------------------------
            # Create a vertical box with two buttons.
            vbox = gtk.VBox()
    
            # Create a button to generate values for A and B.
            b = gtk.Button("Generate A and B")
            vbox.pack_start(b)
            b.connect("clicked", self.generate_variable_values)
    
            # Create a button to view A and B.
            b = gtk.Button("View A and B")
            vbox.pack_start(b)
            b.connect("event", self.button_press)
            # ----------------------------------------------------------------------
            window.add(vbox)
            window.show_all()
        # --------------------------------------------------------------------------
        def quit_event(self, widget=None, event=None):
            """Quit."""
            gtk.main_quit()
        # --------------------------------------------------------------------------
        def generate_variable_values(self, widget=None):
            """Generate values for A and B."""
            self.d_variable_value = {
                "A" : random.randint(0, 10),
                "B" : random.randint(0, 10),
                }
    
            print "I generated " + str(self.d_variable_value)
        # --------------------------------------------------------------------------
        def button_press(self, button, event):
            """button_press method."""
            if event.type == gtk.gdk.BUTTON_PRESS:
                menu = self.create_menu()
                menu.popup(None, None, None, event.button, event.time)
                return True
    
            return False
        # --------------------------------------------------------------------------
        def create_menu(self):
            """Create a menu showing A and B."""
            # How can I update the menu after the user presses the
            # "generate" button?
    
            # If there are no values for A and B, generate them.
            if not self.d_variable_value:
                self.generate_variable_values()
    
            # Show A and B in a menu.
            menu = gtk.Menu()
    
            print self.d_variable_value
            for key, value in sorted(self.d_variable_value.items()):
    
                text = key + " " + str(value)
                item = gtk.MenuItem(text)
                item.show()
                menu.append(item)
    
            return menu
    # ------------------------------------------------------------------------------
    if __name__ == "__main__":
        Generator()
        gtk.main()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not that familiar with COM and was hoping that someone out there, who
I'm not that familiar with Linux so I'm having trouble converting the following command
Since I'm not that familiar with java, I don't know if there's a library
I am not that familiar with core data so im wondering if its possible
I'm not that familiar with PHP so far but already succeeded in registring a
Let me state off the bat that I'm not that familiar with ASP.Net MVC,
I'm new to java and I'm not that familiar with the formatting rules used
I'm not all that familiar with jquery so I'm not quite sure how to
I'm not all that familiar with markup. I tried to use it to test
I am still not so familiar with interfaces in Java. I know that interface

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.