I make a file browser used TreeView, fileSystemModel
Qtreeview.setTooltip is all item set same tooltip
Only Selected Item set Tooltip possible?
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class tree_item(QTreeWidgetItem):
def __init__(self, column, text):
super().__init__()
self.setText(column, text)
self.setToolTip(column, text)
class main(QWidget):
def __init__(self):
super().__init__()
self.tree = QTreeWidget()
self.vbox = QVBoxLayout(self)
self.vbox.setContentsMargins(0,0,0,0)
self.vbox.addWidget(self.tree)
self.tree.setHeaderHidden(True)
self.item1 = tree_item(0, "Item 1")
self.item2 = tree_item(0, "Item 2")
self.item3 = tree_item(0, "Item 3")
self.tree.addTopLevelItems([self.item1, self.item2, self.item3])
self.resize(800,500)
self.show()
app = QApplication([])
window = main()
app.exec()
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class tree_item(QTreeWidgetItem):
def __init__(self, column, text):
super().__init__()
self.setText(column, text)
class main(QWidget):
def __init__(self):
super().__init__()
self.tree = QTreeWidget()
self.vbox = QVBoxLayout(self)
self.vbox.setContentsMargins(0,0,0,0)
self.vbox.addWidget(self.tree)
self.tree.setHeaderHidden(True)
self.item1 = tree_item(0, "Item 1")
self.item2 = tree_item(0, "Item 2")
self.item3 = tree_item(0, "Item 3")
self.tree.addTopLevelItems([self.item1, self.item2, self.item3])
self.tree.currentItemChanged.connect(self.change)
self.resize(800,500)
self.show()
def change(self, e_next, e_prev):
e_next.setToolTip(0,"ToolTip")
if e_prev != None:
e_prev.setToolTip(0,"")
app = QApplication([])
window = main()
app.exec()
Related
The following code creates a standard listbox in tkinter. In normal use, after the user makes the first listbox selection with a mouse click, subsequent selections can be made either by clicking again with the mouse, or by using the up/down cursor keys. Both of these (mouse click and cursor keys) correctly trigger ListboxSelect and change the value of SelectedIndex using the associated function UserClickedSelection(event).
The code also includes a function that allows me to set a listbox selection programmatically. This works perfectly, and correctly changes SelectedIndex using the SetSelection(index) function. But...and this is the problem...after making a selection programmatically, the up/down arrow keys no longer function. The user needs to make the next selection using the mouse, and then the up/down arrow keys once again function normally.
import tkinter as tk
root = tk.Tk()
root.geometry("300x300")
SelectedIndex = 0
def UserClickedSelection(event): # get user selection
global SelectedIndex
SelectedIndex = mylistbox.curselection()[0]
def SetSelection(index): # set selection
global SelectedIndex
mylistbox.selection_clear(0,tk.END)
mylistbox.selection_set(index)
mylistbox.activate(index)
SelectedIndex = mylistbox.curselection()[0]
mylistbox = tk.Listbox(root,activestyle="none")
mylistbox.place(x=0,y=0)
mylistbox.config(selectmode=tk.BROWSE)
mylistbox.config(exportselection=False)
mylistbox.bind('<<ListboxSelect>>',UserClickedSelection)
mylist = ['Zero','One','Two','Three','Four','Five']
mylistbox.insert(0,*mylist)
# Using function: set selection to index 2
SetSelection(2)
root.mainloop()
As you can see, in the code I've used the programmatic function to select index 2 of the listbox. This works fine, but the up/down arrow keys do not work until another selection is made afterwards using a mouse click. So there is clearly a difference between a "real" user selection and my "programmatic" selection. Is there some way to rectify this? I need the up/down arrow keys to work consistently. Thanks in advance for any assistance.
If I understand the problem correctly, you just need to make sure the listbox has focus.
def SetSelection(index): # set selection
...
mylistbox.focus_set()
I'm automation a notepad GUI developed in PYQT5 using PYtestqt. when I try to click the menu bar or toolbar options using qtbot it is not simulating the click
def test_quit(qtbot):
window = MainWindow()
qtbot.add_widget(window)
window.show()
qtbot.wait_for_window_shown(window)
qtbot.mouseClick(window.file_menu, QtCore.Qt.LeftButton)
I was trying to find a way to trigger an action under a menu. Since you most likely want to trigger an action (item under the menu), this may help you as well. Instead of using qtbot, use the window directly and call trigger. So something like this:
def test_quit(qtbot):
window = MainWindow()
qtbot.add_widget(window)
window.show()
qtbot.wait_for_window_shown(window)
window.file_quit_action.trigger()
if you wanna just trigger the click event connected action, you can try this.
In MainWindow class:
# binding action and function
action_open = QAction("Open", self)
action_open.setCheckable(False)
action_open.setObjectName("action_open") # Notice set a object name
action_open.triggered.connect(self.file_open)
In test scripts:
def test_file_open_action(qtbot):
window = MainWindow()
qtbot.add_widget(window)
window.show()
win.findChild(QAction, 'action_open').trigger() # call the method
So you can get the same result after simulate click memubar widget.
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()
I have a Spinner widget with a list of items higher than my screen, so a scroll bar appears.
I want to know if i can change the color of this scroll bar, or how to remove it if not.
I tried to change it with bar_color but no effect in the Spinner..
Spinner itself doesn't have any bar_color. Spinner create a Dropdown which inherit of a ScrollView. What you have to do is to customize the Dropdown class:
from functools import partial
ColoredDropdown = partial(Dropdown, bar_color=(1, 0, 0, 1))
# and then
spinner = Spinner(dropdown_cls=ColoredDropdown)
While adding component dynamically, 'this.container is null' is displayed in firebug.
I have a window with some combo boxes say combo1, combo2, combo3 and a label. Based on the selection value of combo3 the 'label' field is removed and replaced with combobox or text field. i do this my using
form.items.removeAt(4);
form.items.insert(4, newItem); #here newItem can be combox/textfield
form.doLayout();
The form resides inside a panel.
When above lines are execueted. 'this.container is null' is displayed and component fails to insert/add in appropiate position.
Any suggestions?
You should not be modifying the underlying items collection. Use the remove/insert methods on the container.
Try to comment those lines line-by-line to see which one produces error like
form.items.removeAt(4);
//form.items.insert(4, newItem); #here newItem can be combox/textfield
//form.doLayout();
form.items.removeAt(4);
form.items.insert(4, newItem); #here newItem can be combox/textfield
//form.doLayout();
form.items.removeAt(4);
form.items.insert(4, newItem); #here newItem can be combox/textfield
form.doLayout();
Your problem could take place because of inserted/replaced object is no yet prepared when you try to insert it. Give us your newItem initilization code.
Upd
Or you can wrap your changing components (label, combobox, textfields) in a panel with card layout. And on change of combo3 simply select exact card in that panel.