PyQt : get a value from combobox when a button pressed - python

I use qt designer and convert it from *.ui to *.py, I want to make application for send and receive serial data,,
i use combobox to alow user for setting serial comŲ©unication
self.ui.comboBox_2.addItems(['2400','4800','9600','19200'])
my question is how can i get value from combobo_2 to fill serial buadrate when I click a button
this is my code
self.connect(self.ui.comboBox_2, QtCore.SIGNAL('activated(QString)'),ser.baudRate())
and get an error
File "mainw.py", line 18, in press_2 self.connect(self.ui.comboBox_2,
QtCore.SIGNAL('activated(QString)'),ser.baudRate()) AttributeError:
'Serial' object has no attribute 'baudRate'

Your question about using a button to get the value from the combo box is different than what you are currently doing which is using a signal directly from when an value in the combo box was selected.
Your error is related to something else, it looks like in your signal you are calling a function "ser.baudRate()" but you have to pass in a function object, as it will pass in whatever "ser.buadRate()" returns. Which probably isn't a function. I'm not sure what that function returns. In any case, here is some ideas:
Using a button
If you want to use a button, then you would write something like this:
self.connect(self.ui.myButton, QtCore.SIGNAL('clicked()'), self.updateBaudRate)
def updateBaudRate(self):
# get value from combo box
rate = str(self.ui.comboBox_2.currentText()) # convert to string otherwise you will get a QString which is sometimes not friendly with other tools
ser.baudRate(rate)
Using the combo box signal
self.connect(self.ui.comboBox_2, QtCore.SIGNAL('currentIndexChanged(QString)'), self.updateBaudRate)
def updateBaudRate(self, rate):
ser.baudRate(str(rate)) # again convert to string as it my not accept a QString
You could use partial from the functools module or use a lambda instead of writing a function for the signal, but this is just for example.
You probably also want to use the "currentIndexChanged" signal instead of "activated" as "currentIndexChanged" will only emit when the value has changed, otherwise it will signal even if the user didn't select a different value in the combo box.

Related

Unable to send signal from Button created in Python loop

I am trying to create a series of buttons in a loop. Each button gets an id number as it's text and when the button is clicked it is supposed to send the id number to a function that will open an archived order. At this time I just want to print the order number to prove that the signal works and each button is connected to the correct order number.
ui.cmdOpen = QtWidgets.QPushButton(ui.frOrdHist)
ui.cmdOpen.setGeometry(QtCore.QRect(269, line1Y, 61, 22))
ui.cmdOpen.setText(iOrderId)
ui.cmdOpen.setObjectName("cmdOpen")
ui.cmdOpen.clicked.connect(lambda button=ui.cmdOpen:displayOrder(ui, button))
def displayOrder(ui, button):
i = button.text()
print(i)
When I click the button, I get an error message that says "boolean object has no text attribute"
I tried passing the order number directly and it would print "False" so still a boolean. I don't know where the boolean is coming from, it must be something wrong in the signal.
The clicked signal always sends the checked state of the button. So this will overwrite your default button argument with a boolean value, which is why you get the AttributeError (i.e. because bool doesn't have that method). You should instead make the connection like this:
ui.cmdOpen.clicked.connect(
lambda checked, button=ui.cmdOpen: displayOrder(ui, button))
PS: another common source of this issue is the triggered signal of QAction. If you ever find your slots receiving unexpected inputs, it's always worth checking the Qt Docs to see if the signature of the signal has any parameters with default values - i.e. that look something like this:
void QSomeClass::someSignal(bool param = false)

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 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!

Get value from gtk.Entry in PyGTK

I am working on a PyGTK GUI project, and i would like to know, is there any way to get variable value from gtk.Entry?
text=gtk.Entry(0)
text.set_activates_default(True)
port = text.get_text()
This doesn't work as, the text box is empty by default, and get_text() returns empty string, any ideas?
This doesn't work as, the text box is empty by default, and get_text() returns empty string, any ideas?
Sounds like you're looking for getting the text after some user input. In order to do this, gtk uses signals that allow you to connect a user action to some function that will do something. In your case you want this function to get the text from the input. Because you haven't described the user interaction I'll give the simplest example. If you had a button
in your GUI that, when clicked, would grab whatever is typed in the entry at that moment, you'd do this:
button = gtk.Button( 'Click Me')
button.connect( 'clicked', on_button_click )
Then, you define the on_button_click function:
def on_button_click(self, widget, data=None):
port = text.get_text()
print 'Port: %s' % port
So with the sample code above, you'd have a button that, when clicked, grabs the text from your gtk.Entry.
Check out this link for a simple example on how to use signals in pygtk
Since your text field is by default empty, get_text() would return empty string.
Add a button callback function or some other function. So whenever that function is called, you would get the string from the Entry using
text.gtk_text()
You might want to use self here. since you'll be accessing entry in some other function and not in main thread, use
self.text=gtk.Entry()
self.text.set_activates_default(True)
def foo(self,widget,event): # some callback function
port = self.text.get_text()
print port
The retrieving method is the correct one, but right after the creation, the user can't yet have typed anyting into the field.
You would have to wait for the user to actually type something into the field, and afterwards calling get_text().

PyQt QSpinBox update range depending on the value of other spinbox

i am using pyqt4 to develop a GUI for the first time;
I have a spinbox, and I would like the range of values allowed in it to be dependant on the value another spinbox. for example, the maximum value allowed in the first spinbox should be equal to the value of the second spinbox.
I thought this may be possible using a valueChanged() signal to call a method that is something like:
def function
max = spinbox2.value()
spinbox1.setMaximum(max)
but that didnt work, does anyone know how to do this?
thank you
You have not shown your code where you make the connection between the 'valueChanged' signal of spinbox2 with the function. Are you making that connection ?. Also the code you have given for function seems to be incomplete.
You can try something like this:
spinbbox2.valueChanged.connect(handler)
# Or this which works for Python 3.5
spinbbox2.valueChanged[int].connect(handler)
# The function handler called is called whenever
# value in spinbox2 is changed and parameter to
# handler is the new value of spinbox2
def handler(value):
spinbox1.setMaximum(value)

Categories

Resources