For example, lets create a Treeview widget using a class as follows:
class FiltersTree:
def __init__(self, master, filters):
self.master = master
self.filters = filters
self.treeFrame = Frame(self.master)
self.treeFrame.pack()
self._create_treeview()
self._populate_root()
def _create_treeview(self):
self.dataCols = ['filter', 'attribute']
self.tree = ttk.Treeview(self.master, columns = self.dataCols, displaycolumns = '#all')
Populate root, insert children as usual. At the end of the codeblock, you can see where I want to put a Combobox in the tree, using a Combo object:
def _populate_root(self):
# a Filter object
for filter in self.filters:
top_node = self.tree.insert('', 'end', text=filter.name)
# a Field object
for field in filter.fields:
mid_node = self.tree.insert(top_node, 'end', text = field.name)
# insert field attributes
self.insert_children(mid_node, field)
def insert_children(self, parent, field):
name = self.tree.insert(parent, 'end', text = 'Field name:',
values = [field.name])
self.tree.insert(parent, 'end', text = 'Velocity: ',
values = [Combo(self)]) # <--- Combo object
...
Next the class definition of Combo follows. The way I understand it, the combobox widget inherits from and must be placed inside the Labelframe widget from ttk:
class Combo(ttk.Frame):
def __init__(self, master):
self.opts = ('opt1', 'opt2', 'etc')
self.comboFrame = ttk.Labelframe(master, text = 'Choose option')
self.comboFrame.pack()
self.combo = ttk.Combobox(comboFrame, values=self.opts, state='readonly')
self.combo.current(1)
self.combo.pack()
So is this completely wrong? I want to have the ability to change between units (eg m/s, ft/s, etc) from within the Treeview widget.
Any suggestions, plz?
The treeview widget doesn't support embedded widgets. The values for the values attribute are treated as strings.
By default, a Treeview is a static display of a forest of lists of strings. However, with work, after carefully reading Treeview references, one can make a Treeview fairly interactive. For this question, I would bind left click to an event handler that compares the mouse x,y to the bounding box (.bbox) for the units attribute cell. If in the box, display a Combobox, initialized with the current value (such as 'flops'), directly on top of the units attribute cell.
Tkinter.ttk Treeview reference and Tcl/tk treeview reference
Of course, it might be easier to put the Treeview in a frame with with a separate Combobox.
Related
How to find the row number of QFormLayout() from a button which is in that row? I have a delete button on each row of the form. Such that if I click the delete button, that specific row will get deleted. For this, I am planning to use the command QtWidgets.QFormLayout.removeRow(row) command. I have defined the QFormLayout() within my def __init__(self): function like so.
self.attachForm = QFormLayout()
I also have an Add button which calls the self.attachBtn_clicked(self) function given below. So every time the Add button is clicked a new row is added. Any help will be appreciated.
def attachBtn_clicked(self):
hbox = QHBoxLayout()
self.attachForm.addRow('',hbox)
browseBtn = QPushButton("Open")
hbox.addWidget(browseBtn)
addAttachEdit = QLineEdit()
hbox.addWidget(addAttachEdit)
delAttachBtn = QPushButton("x")
delAttachBtn.setFixedSize(15,15)
delAttachBtn.clicked.connect(self.delAttachBtn_clicked)
hbox.addWidget(delAttachBtn)
The objective is now to write the self.delAttachBtn_clicked(self) function which will delete the specific row.
You can iterate through the rows and find the button that matches the sender.
def delAttachBtn_clicked(self):
for i in range(self.attachForm.rowCount()):
if self.sender() == self.attachForm.itemAt(i, QFormLayout.FieldRole).itemAt(2).widget():
self.attachForm.removeRow(i)
return
itemAt(2) is used since delAttachBtn is the 3rd item in each QHBoxLayout.
I am creating a post form that will send my input as dictionary to a server. I planned to display the user entered input in a TextArea with my first display button click. This display button sets the next button (Send) to be visible. The Send button then convert or saves the values to a dictionary (key/value pair).
In my code, i followed the example in the ipywidget documentation (https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20Styling.html [cell 12]) by adding all my widgets in a list. I am have difficulty in calling the values of the widget items to display in the Text area
I have tried to access the values using children[0].values but get error each time. AttributeError: 'Button' object has no attribute 'values'. I will appreciate anyone's help to do this form. If i can get my inputs as a dictionary, that will be very helpful
from ipywidgets import Layout, Button, Box, FloatText, Textarea, Dropdown, Label, IntSlider, Text
#Form item layout
form_item_layout = Layout(
display='flex',
flex_flow='row',
justify_content='space-between'
)
#form container
form_items = [
Box([
Label(value='Name'),
Text(value='John')
], layout=form_item_layout),
Box([
Label(value='Collection UUID'),
Dropdown(options=['theme', 'play', 'character'])
], layout=form_item_layout),
Box([Label(value='Result'),
Textarea(rows = 20)], layout=form_item_layout),
Button(description="display"),
Button(description="Send", button_style='success')
]
#box layout that holds the forms
box_lyt = Layout(
display='flex',
flex_flow='column',
border='solid 2px',
align_items='stretch',
width='50%'
)
form = Box(children=form_items, layout=box_lyt)
display(form)
form.children[4].layout.visibility = 'hidden'
def show(b):
form.children[4].layout.visibility = 'visible'
form.children[2] = c.children[0] + c.children[1] + c.children[2]
#Convert textarea values to dictionary
def list_children(c):
return c.children[2].to_dict()
form.children[3].on_click(show)
form.children[4].on_click(list_children )
I expect a widget displaying: Name, ID, result and display button. Clicking the display should show the values in the result TextArea and make the button (send) visible. Clicking the Send button should accept these values from result and save as dictionary. The widget displays but is not responsiveenter image description here
I have this.
scrollbar = tk.Scrollbar(listbox)
scrollbar.pack(side=RIGHT, fill=Y)
listbox = tk.Listbox(root)
listbox.insert(1,'a')
listbox.pack()
listbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=listbox.yview)
with open('testCUR.csv', newline='') as csvfile:
spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
for row in spamreader:
print(', '.join(row),'\n')
I was wondering how to correctly populate the listbox with values from a .csv file?
And also why I cannot use the scroll bar correctly?
Once the list box is populated I would like to put the value of the last cell from the selection to a variable for use in a URL string. I havent found any tutorials for this so was looking for help here.
I have tried this.
listbox = tk.Listbox(root, height=1)
listbox.place(x=300,y=75)
with open('testCUR.csv', 'r') as f:
reader = csv.reader(f)
your_list = list(reader)
for item in your_list:
listbox.insert(end, item)
or this inputs only first entry
with open('testCUR.csv', 'r') as f:
reader = csv.reader(f)
your_list = list(reader)
for item in your_list:
listbox.insert(1, item)
Once I have all the values in the list box and it is scrollable which I would love if it was just damn.
listbox = tk.Listbox(root, height=1, scroll=auto)
I need to be able to use only the currency code which is the last value in the .csv file and use it as the selected value and then use it in a variable for the url. The .csv file looks like this.
Algeria د.ج DZD
Andorra € EUR
Angola Kz AOA
Anguilla $ XCD
Antigua and Barbuda $ XCD
Argentina $ ARS
Armenia AMD
Aruba ƒ AWG
Ascension Island £ (*none*)
I am also trying pandas but am new to it because it looks much easier and cleaner to use.
csv_file = ('testCUR.csv')
df = pd.read_csv(csv_file)
saved_col = df['CODE']
for item in df:
listbox.insert(end, saved_col)
Always error. NameError: name 'end' is not defined.
Happens with END also
I was wondering how to correctly populate the listbox with values from a .csv file?
To insert text into a listbox you must give it an index to tell it where to insert. The index is a string that is either a number, or the string "end". In your case you used a variable named end, which of course doesn't exist.
You can insert the text like this:
listbox.insert("end", item)
And also why I cannot use the scroll bar correctly?
You haven't described why your scrollbar is not correct.
Making a scrollbar works requires two-way conversation. The scrollbar must be told what widget to scroll (via the command attribute, and the widget needs to know which scrollbar to update when it is scrolled (via the yscrollcommand or xscrollcommand attribute).
It's also good to explicitly set whether the scrollbar is horizontal or vertical, though in your case it's vertical which is the default.
And finally, it's generally the best practice to make the scrollbar and the widget being scrolled to have the same parent. You made the mistake of making the scrollbar a child of the listbox. Instead, make it a child of whatever the listbox is a child of. You also made the mistake of trying to make it the parent of the listbox before you created the listbox. A widget must exist before you can give it children.
Here is how to create the listbox and scrollbar:
listbox = tk.Listbox(root)
scrollbar = tk.Scrollbar(root, orient="vertical", command=listbox.yview)
listbox.configure(yscrollcommand=scrollbar.set)
I want to change the background colour of the treeview headings. I have identified the element option of the Treeview.Heading layout responsible for this: Treeheading.cell. The problem is that this setting does not work on the 'vista' theme (Due to drawing issues I assume).
working code (theme looks terrible though):
from tkinter import *
from tkinter import ttk
p=Tk()
separator = PanedWindow(p,bd=0,bg="#202322",sashwidth=2)
separator.pack(fill=BOTH, expand=1)
_frame = Frame(p,bg="#383838")
t=ttk.Treeview(_frame)
t["columns"]=("first","second")
t.column("first",anchor="center" )
t.column("second")
t.heading("first",text="first column")
t.heading("second",text="second column")
t.insert("",0,"dir1",text="directory 1")
t.insert("dir1","end","dir 1",text="file 1 1",values=("file 1 A","file 1 B"))
id=t.insert("","end","dir2",text="directory 2")
t.insert("dir2","end",text="dir 2",values=("file 2 A","file 2 B"))
t.insert(id,"end",text="dir 3",values=("val 1 ","val 2"))
t.insert("",0,text="first line",values=("first line 1","first line 2"))
t.tag_configure("ttk",foreground="black")
ysb = ttk.Scrollbar(orient=VERTICAL, command= t.yview)
xsb = ttk.Scrollbar(orient=HORIZONTAL, command= t.xview)
t['yscroll'] = ysb.set
t['xscroll'] = xsb.set
print(ttk.Style().theme_names())
ttk.Style().theme_use('default')
ttk.Style().configure("Treeview", background="#383838",foreground="white")
ttk.Style().configure("Treeview.Heading",background = "blue",foreground="Black")
p.configure(background='black')
t.grid(in_=_frame, row=0, column=0, sticky=NSEW)
ysb.grid(in_=_frame, row=0, column=1, sticky=NS)
xsb.grid(in_=_frame, row=1, column=0, sticky=EW)
_frame.rowconfigure(0, weight=1)
_frame.columnconfigure(0, weight=1)
separator.add(_frame)
w = Text(separator)
separator.add(w)
p.mainloop()
my attempt using 'vista' theme:
ttk.Style().element_create("Treeheading.cell","from","default")
ttk.Style().configure("Treeview", background="#383838",foreground="white")
ttk.Style().configure("Treeview.Heading",background = "Blue")
element_create has worked in other instances of this problem but with different widgets.
Thank you, any help would be appreciated.
working in python 3. Also the code is not mine, I found it and used it to test.
You are on the right track but need to change the border element rather than the cell element. As you are working on Windows, the treeview cells are being displayed using a system provided theme element from the Visual Styles API. In this case it is a HP_HEADERITEM part from the HEADER class. As this is drawn by the system theme engine you don't get to customise it from Tk aside from selecting alternate looks according to the state.
If you must customise the look of the header then you have to replace the theme part with one that Tk can customise and the default theme is a good choice. I would also recommend that you define this as a custom style so that you can re-style specific widgets and not necessarily all of them.
style = ttk.Style()
style.element_create("Custom.Treeheading.border", "from", "default")
style.layout("Custom.Treeview.Heading", [
("Custom.Treeheading.cell", {'sticky': 'nswe'}),
("Custom.Treeheading.border", {'sticky':'nswe', 'children': [
("Custom.Treeheading.padding", {'sticky':'nswe', 'children': [
("Custom.Treeheading.image", {'side':'right', 'sticky':''}),
("Custom.Treeheading.text", {'sticky':'we'})
]})
]}),
])
style.configure("Custom.Treeview.Heading",
background="blue", foreground="white", relief="flat")
style.map("Custom.Treeview.Heading",
relief=[('active','groove'),('pressed','sunken')])
What we are doing is defining a new widget style using the same layout as for the standard treeview style and replacing the border element. While we have not defined the other custom elements, these are looked up hierarchically so in the absence of a Custom.Treeheading.text it will use a Treeheading.text.
To use this, we set the style of the treeview widget:
t=ttk.Treeview(_frame, style="Custom.Treeview")
Ends up looking like this on Windows 10:
I am trying to add a QPushButton widget into a QGroupBox such as:
self.btn = QtGui.QPushButton('Push Button')
self.grp_box = QtGui.QGroupBox('Button Section')
self.grp_box.addWidget(self.btn)
When trying to run the code, I got this error : AttributeError: 'NoneType' object has no attribute 'addWidget'
After some online checking, it seems that QGroupBox only allows setLayout, meaning I will need to use QVBoxLayout or QHBoxLayout etc.
Is there anyway to get around this, adding in a widget without the use of any layout(s)? I am using PyQt.
First create your main layout = QHBoxLayout()
main_layout = QHBoxLayout()
Then create the group box:
group_box = QGroupBox("Group Box")
Create the group box layout:
group_box_layout = QVBoxLayout()
Add widgets to the group box layout like this:
group_box_layout.addWidget(QCheckBox("Check Box 1"))
group_box_layout.addWidget(QCheckBox("Check Box 2"))
group_box_layout.addWidget(QCheckBox("Check Box 3"))
Assign the group box layout to the group box:
group_box.setLayout(group_box_layout)
Assing the group box to the main layout:
main_layout.addWidget(group_box)
And add this at the end:
widget = QWidget()
widget.setLayout(layout1)
self.setCentralWidget(widget)