I'm working on an application in PyQt that takes an object dictionary and allows you to plot the variables streaming from a robot in real time. One of the things I'm working on to enable this is a drop down menu. Unfortunately, we have a couple hundred variables, so my PyQt Combobox pops up from the top of the screen to the bottom with items when clicked. I'd like to limit the number of items displayed at a time to 20, with the ability to scroll to see the rest. I've tried using the documented setMaxVisibleItems method, but it doesn't affect the drop down at all. Any recommendations?
Code here:
#!/usr/bin/env python
from PyQt4.QtCore import Qt
from PyQt4.QtGui import QComboBox, QApplication
from cli.parc2_od import cli_od
import sys
app = QApplication(sys.argv)
items = cli_od.OD.keys() #Items to populate dropdown.
combo = QComboBox()
#The solution:
combo.setStyleSheet("QComboBox { combobox-popup: 0; }")
combo.setMaxVisibleItems(10)
combo.addItems(items)
combo.resize(300, 30)
combo.show()
sys.exit(app.exec_())
According to the documentation:
The property maxVisibleItems is ignored for non-editable comboboxes in styles that returns true for `QStyle::SH_ComboBox_Popup such as the Mac style or the Gtk+ Style.
And you can override that SH_ComboBox_Popup style hint with a style sheet:
combo.setStyleSheet("QComboBox { combobox-popup: 0; }");
Related
I tried following the code to merge a dropdown menu ipywidget into the ipysheet. it seems like my code works the way I imagined. However, I am unable to select items from the menus listed in the dropdown widget.
import ipysheet
import ipywidgets as widgets
Solvent = widgets.Dropdown(
options=['DMC', '2-Butanol', 'Chloroform', 'Ethanol'],
value='DMC',)
sheet2 = ipysheet.sheet()
ipysheet.column(0, [Solvent])
ipysheet.column(1, [1,2,3,4, 5])
widgets.VBox([sheet2, Solvent ])
After some review, I believe that this is a genuine bug/shortcoming with ipysheet. Though there are alternative approaches, this inability to use a particular widget within ipysheet cells is not an expected result of this API... Someone please correct me if I'm missing something.
There is a "hack" that is working for me through Jupyter Notebook in the browser, which is to right-click the dropdown before left-clicking it to change the context of the dropdown menu, allowing you to select it via mouseover.
The other alternative is passing in your options as the "choice" argument to the cell you wish to contain the dropdown.
from ipywidgets import link
import ipysheet
import ipywidgets as widgets
Solvent = widgets.Dropdown(
options=['DMC', '2-Butanol', 'Chloroform', 'Ethanol'],
value='DMC',)
sheet2 = ipysheet.sheet()
# dropdown_cell = ipysheet.cell(0,0,choice = Solvent.options,value='WORLD')
interactive_cell = ipysheet.cell(1,3, value='HELLO')
dropdown_cell = ipysheet.cell(0,0,choice = Solvent.options,value='WORLD')
# link((interactive_cell,'value'),(dropdown_cell,'value'))
link((dropdown_cell,'value'),(interactive_cell,'value'))
widgets.VBox([sheet2, Solvent ])
From my current understanding (and attempts), only cells are interactive in ipysheets. (The alternative method of using the ipysheet "calculation" decorator also depends on the items being Cells, not Columns.)
I want to show the text from the 'Yes' and the 'No' button in the informative text of a QMessageBox, but I don't see how I can get these labels from the buttons.
from PyQt5.QtWidgets import *
import sys
app = QApplication(sys.argv)
msgbox = QMessageBox()
msgbox.setStandardButtons(msgbox.Yes | msgbox.No)
info_text = "Click '{yes}' to confirm. Click '{no}' to abort."
msgbox.setInformativeText(info_text)
if msgbox.exec_() == msgbox.Yes:
print("Confirmed")
else:
print("Aborted")
By calling setStandardButtons, the button order and the button labels will be set to the default for the current operating system and the current language setting. How can I obtain these defaults so that I can use them for the slots in the string info_text?
I thought about using the buttons attribute from the QMessageBox object, which is a list of QPushButton objects. I can read the labels from there, but I don't see how I could determine whether the first element in the list is the Yes or the No button.
Okay, I was being stupid: along side the buttons attribute, there is also the button() method, which takes as its argument the button type that I want to retrieve. I can then use text() to get the label. Finally, the hotkey marker & has to be stripped from the label:
info_text = "Click '{yes}' to confirm. Click '{no}' to abort.".format(
yes=msgbox.button(msgbox.Yes).text().replace("&", ""),
no=msgbox.button(msgbox.No).text().replace("&", ""))
msgbox.setInformativeText(info_text)
I have an issue with curselection of ListBox. It appears in Python 3.6. In Python 3.4 everything works fine. My code selects items from ListBox and put it in Entry widget. It works perfectly well when i click on the items in ListBox. But sometimes (after 5-10 clicks) when I click in Entry widget an error appears:
_tkinter.TclError: bad listbox index "": must be active, anchor, end, #x,y, or a number
Here's my code sample:
from tkinter import *
def insert_into_entry(event):
index=list_box.curselection()
print(index)
selected_item=list_box.get(index)
entry1.delete(0,END)
entry1.insert(END,selected_item)
window=Tk()
entry=Entry(window)
entry.grid(row=0,column=0)
list_box=Listbox(window,height=5,width=45)
list_box.grid(row=1,column=0)
list_box.bind('<<ListboxSelect>>',insert_into_entry)
entry1=Entry(window)
entry1.grid(row=2,column=0)
a=['one','two','three','four']
for i in a:
list_box.insert(END,i)
window.mainloop()
I tried to examine changes in index value. And error brings about when after clicking in Entry widget index returns empty tuple. It's my first question so I'll be greatfull for every response.
From what I can see by testing <<ListboxSelect>> I believe that the <<ListboxSelect>> event activates every time the selection changes even if that selection changes to "nothing selected". I believe this is why when you click off of the listbox and into any other field and the <<ListboxSelect>> event is triggered your insert_into_entry function is getting an empty tuple from curselection().
According to the documentation here the courselection() method will return an empty tuple if nothing is selected.
To avoid any errors we can first check if the results of ndex = list_box.curselection() are not equal to () an empty tuple. If not then perform the rest of the function. This should prevent any errors caused by this behaviour with the <<ListboxSelect>> event being called outside of the listbox.
Oh and one other thing. Don't use built in names for variable names. change index = list_box.curselection() to something like ndex = list_box.curselection()
from tkinter import *
window=Tk()
def insert_into_entry(Event = None):
ndex = list_box.curselection()
if ndex != ():
selected_item=list_box.get(ndex)
entry1.delete(0,END)
entry1.insert(END,selected_item)
entry=Entry(window)
entry.grid(row=0,column=0)
list_box=Listbox(window,height=5,width=45)
list_box.grid(row=1,column=0)
list_box.bind('<<ListboxSelect>>', insert_into_entry)
entry1=Entry(window)
entry1.grid(row=2,column=0)
a=['one','two','three','four']
for i in a:
list_box.insert(END,i)
window.mainloop()
With this code:
QtCore.QObject.connect(self.tabWidget, QtCore.SIGNAL("tabCloseRequested(int)"),
self.tabWidget.removeTab)
I can close any tab QTabWidget, and the names of these tabs are:
work_1
work_2
work_3
But I want the tab work_1 never closes.
Use Index did not work for two reasons:
The tabs can be dynamically moved by this code:
self.tabWidget.setMovable (True)
That makes the Index are constantly changing.
The user has the ability to add new tabs.
Tabs can be identified by their widgets, and the widgets can be identified by their objectName (or some other unique attribute):
self.tabWidget.tabCloseRequested.connect(sef.removeTab)
...
def removeTab(self, index):
widget = self.tabWidget.widget(index)
if widget is not None and widget.objectName() != 'work_1':
self.tabWidget.removeTab(index)
or perhaps more simply:
if widget is not None and widget is not self.work_1:
self.tabWidget.removeTab(index)
We're building a GUI interface with Python+tkinter.
The problem is when we want to set the view mode of an entity. I need to set the view mode or state of the treeview widget as 'disabled'.
How can we solve it?
Thanks for any support.
UPDATE
self.frmTab01.trvDetailorder.configure(selectmode='none')
I'm looking for a solution in which appart from disable the selection, affect the visualization of the widget just like an entry widget.
nbro is right, you need to change the Treeview style to make it look disabled. In addition, I also deactivated the possibility to open/close items when the Treeview is disabled using binding tricks on the mouse click.
In my example I added an entry so that you can compare the look on the two widgets. If you are using OS X or Windows, you might need to change the theme (style.theme_use("clam") should do) because their default themes are not very customizable.
from tkinter import Tk
from tkinter.ttk import Treeview, Style, Button, Entry
root = Tk()
def toggle_state():
if "disabled" in tree.state():
e.state(("!disabled",))
tree.state(("!disabled",))
# re-enable item opening on click
tree.unbind('<Button-1>')
else:
e.state(("disabled",))
tree.state(("disabled",))
# disable item opening on click
tree.bind('<Button-1>', lambda e: 'break')
style = Style(root)
# get disabled entry colors
disabled_bg = style.lookup("TEntry", "fieldbackground", ("disabled",))
disabled_fg = style.lookup("TEntry", "foreground", ("disabled",))
style.map("Treeview",
fieldbackground=[("disabled", disabled_bg)],
foreground=[("disabled", "gray")],
background=[("disabled", disabled_bg)])
e = Entry()
e.insert(0, "text")
e.pack()
tree = Treeview(root, selectmode='none')
tree.pack()
tree.insert("", 0, iid="1", text='1')
tree.insert("1", 0, iid='11', text='11')
Button(root, text="toggle", command=toggle_state).pack()
root.mainloop()