I'm using pyqt and wanted to display different images with each item of a combobox. It doesn't have to be a qcombobox, any form of a drop down menu will do.
Appretiate any answers/ links to examples.
Thank you
Yes, you can do this using QComboBox:
combo = QComboBox()
combo.addItem(QIcon("path/to/image.png"), "Item 1")
combo.addItem(QIcon("path/to/image2.png"), "Item 2")
...
Related
I am looking for a PysimpleGUI way to create dropdown menus.
Currently, I can create ComboBoxes as such:
import PySimpleGui as sg
sg.Combo(list1, size = params)
This creates an Input box and a dropdown list with a slider populated by list1 elements.
However, my users are very sloppy with inputs and I'd like to restrict them to a simple dropdown list.
Now I know I could maybe use the ListBox element but it doesn't have a slider/seems much less versatile than Combo.
What other arguments could I pass to Combo to make this work?
NB:In the documentation they state that Combo == InputCombo == Drop == DropDown but without giving more details but it confuses me a lot.
There is no slider when ListBox is has not enough entries however one appears when enough are added.
sg.Listbox(list(df_names.NAMES), size=(20,4), enable_events=False, key='_LIST_')
Set the readonly to True
sg.Combo(..., readonly=True)
This is me trying to get a ListBox with a Scrollbar. I've read about 20 other answers on this site and none seem to work in my situation, so I have to ask my own.
I have a LabelFrame widget in which I have a few labels and a ListBox widget. I want the ListBox widget to have a scrollbar. I cannot seem to get it to work.
Here is the offending code:
self.lbDates = tk.Listbox(master=group, width=12)
self.lbDates.grid(row=3, column=1, padx=3, pady=3)
self.scrollDates = tk.Scrollbar(self.lbDates, orient="vertical")
self.scrollDates.config(command=self.lbDates.yview)
self.scrollDates.grid(row=3, column=1)
self.lbDates.config(yscrollcommand=self.scrollDates.set)
A few questions: is the scrollbar supposed to be in the same space (i.e. the same grid row and column) as the ListBox widget? I supposed it should be, but given how much trouble I've had with this, I feel I should ask.
I should say that the list box is populated by pressing a button elsewhere. Without the scrollbar in place, this date is populated correctly so I'm assuming the problem isn't there.
What happens when I run this code? Well I don't see a ListBox, but I to see the scroll bar. Now when there's supposed to be data in there (and I know because I populate it), I see what you might imagine is the correct scrollbar, although the ListBox is either invisible or hidden or something. It flickers on for a fraction of a second when it's populated but then is hidden again.
I can show you, in fact, an example of this.
The top one has no data in and the second has data in. Of course you can't see the data, but you can see that it's supposed to be there. And if I remove the scrollbar, I do get the data I expect in the ListBox widgets. I would like to show an image of the moment it populates before it disappears but it seems like a single frame and I can't catch it.
I'm sure my mistake must be something very simple here, and I very much hope someone can assist. I am using tkinter for the widgets and Python 3.7.
Edit: I have made some alterations as suggested below, so that my ListBox and Scrollbar now exist in a Frame and that Frame is within the LabelFrame object. As follows:
fr = tk.Frame(master = group)
fr.grid(row=4, column=1)
self.lbDates = tk.Listbox(master=fr, width=12)
self.lbDates.pack()
self.scrollDates = tk.Scrollbar(master=self.lbDates, orient="vertical")
self.scrollDates.config(command=self.lbDates.yview)
self.scrollDates.pack()
self.lbDates.config(yscrollcommand=self.scrollDates.set)
Unfortunately there is no difference and I seem to be experiencing the exact same problems as before.
is the scrollbar supposed to be in the same space (i.e. the same grid row and column) as the ListBox widget?
It's up to you. Typically you do not put the scrollbar inside the listbox, as it would obscure some of the data in the listbox. The listbox and scrollbar will have the same parent. If you're using grid, usually the vertical scrollbar belongs in the column immediately to the right of the listbox.
What happens when I run this code? Well I don't see a ListBox, but I to see the scroll bar.
That is because the listbox shrinks to fit its children. The listbox is there, but it's only as wide and tall as the scrollbar. This is another reason why you shouldn't put the scrollbar inside the listbox.
If you want the scrollbar to appear as if it's inside the listbox, the best practice is to create a frame that holds the listbox and scrollbar.
You can make the frame sunken and the listbox flat, and it will give the illusion that the scrollbar is inside the listbox. You can then put that frame anywhere you like in your UI and the scrollbar will always be with the listbox.
Both the listbox and the scrollbar need to be children of the frame for this to work.
Here's a simple example:
import tkinter as tk
root = tk.Tk()
lb_frame = tk.Frame(root, bd=1, relief="sunken")
listbox = tk.Listbox(lb_frame, bd=0, yscrollcommand=lambda *args: vsb.set(*args))
vsb = tk.Scrollbar(lb_frame, orient="vertical", command=listbox.yview)
vsb.pack(side="right", fill="y")
listbox.pack(side="left", fill="both", expand=True)
lb_frame.pack(padx=20, pady=20)
for i in range(1, 100):
listbox.insert("end", f"Item {i}")
root.mainloop()
Hi guys I'm making this chat app with tkinter and it works perfectly except by the fact that the text I post in my Listbox, don't shows up the way I want. Once the space in the Listbox finishes, I have to manually scroll down to see recent messages. My question is, what can I do to make the Listbox automatically scroll down when it gets fill. This is the part of the code of the Listbox
lista=Listbox(raiz,font=('Arial'))
lista.pack(side=LEFT,padx=10,pady=10,ipadx=200,fill=X,expand=True )
And this is where I posted:
def post(text,n=0):
winsound.Beep(400,150)
if n==0:
lista.insert(END,text)
campo.delete(0,1000)
else:
for i in text:
lista.insert(END,i)
If you can help me it will be great. Thanks!!!
You need add this line lista.see(tkinter.END) before lista.insert(END,text)
You should clear the Listbox widget before you display the current or insert
def post(text,n=0):
winsound.Beep(400,150)
if n==0:
lista.delete(0, END) # this to clear the listbox and display the current content in the listbox
lista.insert(END,text)
campo.delete(0,1000)
else:
for i in text:
lista.insert(END,i)
Or you can place the new content at top of Listbox like this,
def post(text,n=0):
winsound.Beep(400,150)
if n==0:
return
lista.insert(n-1, text)
campo.delete(0,1000)
else:
for i in text:
lista.insert(END,i)
Since you didn't post your full code or minimal am assuming this how it is.
i have this working example
now i want to be able to edit the QLineEdit and highlight items that are not in the dropdown listview,(eg. red background color) , and add auto complete feature. from my understanding, the only viable way is making a custom widget, with a QTextEdit and a listView ( since qlineedit can't display html ?)
edit: eg I have India,China in the lineEdit, but i only want China's bg color to be red since it's not in the dropdown items list
is there an easier way other than this? thanks !
the code is here: http://pastebin.com/WGrj3ud5
and here : http://www.barishcb.com/?p=426
For the auto-completion, you need to add a QCompleter to your QlineEdit
For the red background, you can dynamically change the style sheet of the QLineEdit:
# in case of item not in the dropdown listview:
self.lineEdit.setStyleSheet("background-color: rgb(255, 0, 0);")
# otherwise:
self.lineEdit.setStyleSheet("")
How to add 2nd childform to mdi parent form "PANEL Control"...??
I am adding child forms to Panel Control of mdi parent form....
i have two child forms, childForm1, childForm2....
when i click a button on mdi parent form, childForm1 is added to mdiParent "PANEL Control"...
Know i want to add childForm2 to mdiform "Panel Control", when i click a button on childForm1...
Means i want to add childForm2 from childForm1 button click event, to mdiParent forms "Panel Control"...???
Can anyone Help me....???
Thanks in advance.....
Use tablelayoutpanel with two columns, in first column show the child1 clicking on second button show next child(2) in next column and hide first column(Column width=0)