Python Gtk3, How can I insert a Response (Button) to MessageDialog? - python

some times I need to insert new response (button) to MessageDialog but I don't know how I can do this. for example msg_dialog.insert_response(Gtk.STOCK_OK, Gtk.ResponseType.OK, 2)
Thanks

The method you are looking for is Gtk.Dialog.add_button:
Adds a button with the given text and sets things up so that clicking
the button will emit the Gtk.Dialog ::response signal with the given
response_id. The button is appended to the end of the dialog’s action
area. The button widget is returned, but usually you don’t need it.
If you want to add several buttons, then you can use Gtk.Dialog.add_buttons:
The add_buttons() method adds several buttons to the Gtk.Dialog using
the button data passed as arguments to the method. This method is the
same as calling the Gtk.Dialog.add_button() repeatedly.
The button data pairs - button text (or stock ID) and a response ID integer are passed individually. For example:
dialog.add_buttons(Gtk.STOCK_OPEN, 42, "Close", Gtk.ResponseType.CLOSE)
will add “Open” and “Close” buttons to dialog.

Related

How to speed up browsing application via pywinauto?

From application that I want to automate I get pane with set of buttons. Clicking on the button adds new buttons set to Pane, and deletes other buttons from source set. Text in buttons are unique in one set, but aren't unique between them. Text on button is the only property I can use to handle it and click, so after clicking, the button is appended to list that contain already clicked buttons. Difference between children of pane and that array are buttons with unique text.
So my function is like:
def click_button(textOnButton, clickedButtons):
children = paneWindow.children() # get buttons currently present
handles = []
for item in children:
handles.append((item.handle, item.texts()[0]))
unique_buttons = list(set(handles) - set(clickedButtons)) # remove clicked buttons
button = [t for t in handles if t[1].startswith(textOnButton)]
paneWindow.childWindow(handle=button[0][0]).click_input()
clickedButtons.append((button[0][0], button.[0][1]))
clickedButtons = []
click_button("FOO", clickedButtons)
click_button("IPSUM", clickedButtons)
It works correctly, but just one function execution takes.. 2 to 3 seconds what is certainly unacceptable. Most timeconsuming is getting pane children and clicking on button. Anyone has an idea how to speed it up?
It's considerable for me to change tool from pywinauto to something else. Python isn't required.

Python Tkinter: Changing other button's text from event handler of one button

I have the following problem when using tkinter to create a very simple window containing a matrix of buttons: When one of the buttons is clicked, the event handler changes the text of that button using the configure method on the button widget. This works. But I also want to change the text in one of the other buttons, which does not work. The method I use is that on creating the button, I store the object returned by the Button method before I use the grid geometry manager to place it. This object looks like ".123456789L" when printed and seems to be a pointer to the widget. I also use configure on this to change the button text. But somehow it seems to be wrong, because it works sometimes, and most of the times not. There's unfortunately no error message, just nothing happens when calling configure. I checked and it seems to be the correct pointer to the widget. Do I have to use a special way to affect a widget other that the one that called the event handler? These are the relevant parts of the code:
# CREATING THE BUTTONS:
buttons={} # global
for i in range(3):
for j in range(3):
button = Tkinter.Button(self,text='foo')
buttons[button]=(i,j)
button.grid(column=j,row=i)
button.bind( "<Button-1>", self.OnButtonClick )
# CHANGING BUTTONS:
def find_button(i,j):
"""Return the pointer to the other button to be changed when a button has been clicked."""
for button,key in buttons.items():
if key==(i,j): return button
def OnButtonClick(self,event):
print "You clicked the button",buttons[event.widget]
i,j=buttons[event.widget]
old_button=find_button(i,j) # This is simplified, I don't actually pass i,j, but other values. But I checked this and it returns the reference to the correct button. But this simplified version works the same way, just assume a different button that the one pressed would be returned.
old_button.configure(text = 'blabla') # THIS DOES NOT WORK
event.widget.configure(text = 'something') # THIS WORKS
I have the same problem and i solve it with:
buttons[button]=(i,j,button)
and in the function OnButtonClicK:
i,j,old_button=buttons[event.widget]
old_button.configure(text = 'blabla') # THIS DOES NOT WORK

getting the label value of a button in Gtk

Am learning Gtk. I wanted to build a calculator, in which i want to display the number pressed , in the textbox. I have completed it, by calling different functions for different buttons clicked, and appending the value in the textbox with the value of the button pressed. Using python 2.7.3
Is there a way to obtain the label value of the button pressed so that i can use a single function instead of 10 functions from 0 to 9?
Thanks in advance
Button callbacks include the widget itself, and you can also pass data. See here.
instead of reading the label of the GtkButton, which is pretty much error prone, you should associate the value represented by the button to the button instance itself, e.g.:
button = Gtk.Button(label='1')
button._value = 1
# add button to the container
button.connect('clicked', on_button_clicked)
button = Gtk.Button(label='2')
button._value = 2
# add button to the container
button.connect('clicked', on_button_clicked)
and then read the value from the button instance inside the signal handler, e.g.:
def on_button_clicked(button):
print 'you pressed the button of value: %d' % (button._value)
GtkWidget instances in Python are Python object, and thus behave like any other Python object.

How to make GtkMenuToolButton open the same menu when 'clicked' signal is emitted?

I am using GtkMenuToolButton and it has a button and a menu. When you click on the arrow the menu is opened. I'd like to make the button open that same menu as well. Simply emitting "show-menu" in the "clicked" callback did not work. Please help how to make this work.
I have currently ended up doing this:
Instead of GtkMenuToolButton I have GtkToolItem with custom content
In custom content I have GtkMenuButton
Inside that one, I delete the default GtkArrow and replace it with 1x2 GtkGrid which has a Label + GtkArrow
As a whole it does what I want =)
When you create the menu, save a reference to it as self.tool_button_menu or something; then in the clicked callback, call
self.tool_button_menu.popup(None, None, None, None, 0, Gtk.get_current_event_time())
The first two Nones are the parent menu and the parent menu item (not applicable). The second two Nones are a positioning callback function (more on that in a minute) and data to pass to it. 0 is the mouse button if the menu was initiated by a mouse button press (but you should pass 0, because I think in your case it's either a mouse button release or a key press.) And the last parameter is the timestamp to give to the menu popup event.
Now the positioning function. It takes two parameters and returns three:
def positioning_function(menu, data=None):
# ...magic...
return x, y, push_in
push_in should be True if you want the menu to be repositioned so that it always fits on the screen. Seems like a good idea. You can get good values for x and y by looking at the tool button's get_allocation(); read the x, y, width and height attributes of that object and calculate a nice place to put the menu.

How to add event listener to dynamic QTableWidgetItem in PyQt4?

I am a Python and Qt newbie...
I am working on a GUI program that shows a list of data using QTableWidget at the beginning.
The rows are inserted into the table using the setItem() method. They are QTableWidgetItem objects.
Now I want to allow users to click-select a certain row (the QTableWidgetItem), and my program will populate a secondary QTableWidget.
I have learnt that there is a thing called Signal and Slot. Am I going to use that?
There are also examples of using installEventFilter(), but it is not appropiate for the QTableWidgetItem.
The easiest way for this would just be to use the itemClicked-signal of the QTableWidget:
def handle_item_clicked(tableItem):
#the callback, called when an item is clicked, with the item as an argument
#populate the secondary table in some way
#register the callback with the QTableWidget
yourTableWidget.itemClicked.connect(handle_item_clicked)
You could also use the currentItemChanged signal which gives you the current and previous selection (e.g. so you can clear or hide your secondary widget if the user deselects an item, in which case the current item will be None).
Qt doesn't have "event listeners". As you mentioned, you do such things by connecting signals to slots.
For example:
QObject.connect(myTableWidget, QtCore.SIGNAL("clicked()"), self.foobar)
This will cause the method self.foobar() to be called when you click on myTableWidget. The foobar function could retrieve which item is selected and populate the second widget based on that.
Take note that in the last argument of connect, you don't use the parentheses (foobar()), just the method name!

Categories

Resources