PySide, how to setup QMessageBox text format? - python

I want to setup QMessageBox text format, but I can't find a valid method.
QtGui.QMessageBox.information(
self,
"Confirm delete!",
"Are you sure you want to delete file?\n %s" % filename,
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No,
QtGui.QMessageBox.No
)
I want to make bold,like:
"Are you sure you want to delete file?\n <b>%s</b>"
How can I do this?

According to PySide's documentation you can use the setTextFormat to specify the Qt.RichText format.
However this will only work when you create a custom QMessageBox instance and then set it up, it will not work when using the QMessageBox.information static-method.
By default QMessageBox uses the Qt.AutoText format which tries to detect whether the text is rich text by using the Qt.mightBeRichText function (I could only find Qt's documentation). The documentation of that function states:
Returns true if the string text is likely to be rich text; otherwise
returns false.
This function uses a fast and therefore simple heuristic. It mainly
checks whether there is something that looks like a tag before the
first line break. Although the result may be correct for common cases,
there is no guarantee.
Unfortunately in your message you have a line break \n before any tag, and hence the function fails to detect your message as rich text.
In any case once you interpret the text as rich text you have to use the HTML line break <br> so using the message:
"Are you sure you want to delete file?<br> <b>%s</b>"
will make the QMessageBox auto-detect the rich text and produce the result you want.

Related

Selenium find element with variable value

i want to click an element with a value, which will be inserted by user. I tried this:
def transition(action, value=none):
if (action=='next_page'):
button = driver.find_element_by_link_text('"value"')
button.click()
transition('next_page', value='car1')
The problem is that 'car1' value isn't inserted. What can i fix it?
What your code does, is actually calling the find_element function with the string "value", as you wrote it in quotation marks, making it a string literal
button = driver.find_element_by_link_text('"value"')
What you want to do is insert the name of the variable value without any "double" or 'single' quotation marks, like so.
button = driver.find_element_by_link_text(value)
As you are having a hard time dealing with such simple python syntax, I recommend you first learn the bare basics of the language before trying to write applications with it. There are a lot of free resources out there.
I can recommend W3Schools as a website with text and interactive examples or if you are more of the video type of learner, I can recommend the course by freeCodeCamp.org or a video series by Tech With Tim. Though the last one didn't age really well.

how to activate a function when a value from a drop down list is selected in python [duplicate]

Using pyqt4 and python 2.6, I am using a qcombobox to provide a list of options. I am having problems with using the selected option. I have been able to use a signal to trigger a method when the option is selected, but the problem is that when the user clicks run, the contents of several of these comboboxes need to be taken into account. So basically I need to get the selected contents of a combobox as a string. Thus far I have only been able use this:
print combobox1.currentText()
to get this:
PyQt4.QtCore.QString(u'Test Selection2')
when all I really want is the 'Test Selection' bit, any ideas?
My combo box was made like this:
combobox1 = qt.QComboBox()
combobox1.addItems(['Test Selection1', 'Test Selection2'])
mainLayout.addWidget(combobox1, 0, 0)
You can convert the QString type to python string by just using the str
function. Assuming you are not using any Unicode characters you can get a python
string as below:
text = str(combobox1.currentText())
If you are using any unicode characters, you can do:
text = unicode(combobox1.currentText())
Getting the Text of ComboBox when the item is changed
self.ui.comboBox.activated.connect(self.pass_Net_Adap)
def pass_Net_Adap(self):
print str(self.ui.comboBox.currentText())
PyQt4 can be forced to use a new API in which QString is automatically converted to and from a Python object:
import sip
sip.setapi('QString', 2)
With this API, QtCore.QString class is no longer available and self.ui.comboBox.currentText() will return a Python string or unicode object.
See Selecting Incompatible APIs from the doc.
If you want the text value of a QString object you can use the __str__ property, like this:
>>> a = QtCore.QString("Happy Happy, Joy Joy!")
>>> a
PyQt4.QtCore.QString(u'Happy Happy, Joy Joy!')
>>> a.__str__()
u'Happy Happy, Joy Joy!'
Hope that helps.

listctrl new line for an data item

I have created a listctrl with some of the data in the listctrl are very long, and instead of showing all of the text it ends with .... For example Att PSSM_r1_0_T is [-10.179077,0.944198]|Att PSSM_r1_0_Y is.... How would i be able to make it so it shows all of the text. Something like
Att PSSM_r1_0_T is [-10.179077,0.944198]|Att PSSM_r1_0_Y is
[-4.820935,9.914433]|Att PSSM_r1_2_I is [-8.527803,1.953804]|Att PSSM_r1_2_K is [-12.083334,-0.183813]|Att PSSM_r1_2_V is
[-14.112536,5.857771]|1
As the text is very long I would prefer if it covered more than one line.
I don't think that is possible to do with a standard listctrl.
Try poking around at the UltimateListCtrl, being a full owner drawn listctrl it has the ability to change the way its looks far more than a standard listctrl.

AssertionError - Selenium/Python

I am creating a Python script with Selenium. I want to run a specific test that checks the default text of a textbox when the page loads up. Below is my code.......
try:
self.assertEqual("Search by template name or category..", sel.get_text("//table[#id='pluginToolbarButton_forms']/tbody/tr[2]/td[2]/em"))
logging.info(' PASS: text box text is correct')
except Exception:
logging.exception(' FAIL: text box text is incorrect')
Here is my error......
self.assertEqual("Search by template name or category..", sel.get_text("//table[#id='pluginToolbarButton_forms']/tbody/tr[2]/td[2]/em"))
File "C:\Python27\lib\unittest\case.py", line 509, in assertEqual
assertion_func(first, second, msg=msg)
File "C:\Python27\lib\unittest\case.py", line 502, in _baseAssertEqual
raise self.failureException(msg)
AssertionError: 'Search by template name or category..' != u'Submitter Requests'
Am I using the wrong function?
Your AssertionError states that the assertion you tried (that's the self.assertEqual(...) in your first code example) failed:
AssertionError: 'Search by template name or category..' != u'Submitter Requests'
This assertion explains that the string 'Search by template name or category' is different from 'Submitter Requests', which is correct ... the strings are, in fact, different.
I would check your second parameter to self.assertEqual and make sure that you're selecting the right feature.
This looks like you are using the right function, but perhaps you are not running your tests in the correct fashion.
The problem seems to be that you are not selecting the right element to compare with. You are basically telling the program to match that "Search by template name or category.." is equal to the contents of whatever is in:
//table[#id='pluginToolbarButton_forms']/tbody/tr[2]/td[2]/em
Apparently, the contents are "Submitter Requests", i.e not what you would expect, so the test fails (as it should). You might not be selecting the right element with that XPath query. Maybe a CSS query would be best. You can read about element selectors in the Selenium documentation.
Keep an eye open for a pitfall as well: the text returned by Selenium is a Unicode object, and you are comparing it against a string. This might not work as expected on special characters.

Changing the title of a Tab in wx.Notebook

I'm experimenting with wxPython,
I have a tabbed interface (notebook) and each tab is basically a file list view (yes, I'm trying to make a file manager)
The file list inherits from wx.ListCtrl, and the tabbed interface inherits from wx.Notebook
I'm just starting .. and I had it so double clicking on a folder will cd into that folder, but I want to also change the title of the tab.
How do I do that?
I have the object that represents the file list and the title I want to set it to,
[ EDIT Notebook.SetPageText() takes a number, so I can't pass the tab object directly to it ]
my current approach is to cycle through the tabs until one of them matches my tab:
for tab_id in range(self.GetPageCount()):
if self.GetPage(tab_id) == tab:
self.SetPageText(tab_id, title)
break
This seems rather naive though, isn't there a smarter approach?
I don't know wxPython, but I assume it wraps all the methods of the C++ classes.
There is wxNotebook::GetSelection() which returns wxNOT_FOUND or the index of the selected page, which can then be used to call wxNotebook::SetPageText().
Or use wxNotebook::GetPage() with this index to check whether it is equal to tab.
I think doing something like this helps :
notebook.get_tab_label(notebook.get_nth_page(your_page_number)).set_text("Your text")
If you want to have a reference to the current tab always, you must connect the "switch-page" signal, and save the page in a variable.
As .GetPage returns a wx.Window, I think tab.Label = title should work.

Categories

Resources