I would like to display a list of about 2500 fonts with 2 columns. Column 1 with the font name and column 2 with a sample text styled with the font named in column 1 for that row.
I am trying to use tkinter treeview to do this. I saw a similar question asked here but the answer that was given only applied to column headers.
I am also open to suggestions for other widget structures to accomplish this.
for font_name in font_list:
# create a tag for each font name in the list
# the name of the font is the name of the tag
my_treeview.tag_configure(font_name, font=font_name)
# insert items into the treeview using the font_name tags
my_treeview.insert(
'',
tk.END,
values=(font_name, sample_text),
tags=[font_name]
)
# the caveat here is that both columns will be in the new font
Related
What I really want is for the imported money numbers to be formatted with red font for negative, black for positive, with a dollar sign ($) in front.
I can't seem to find anyone else struggling with this. I have a hard time imagining I'm the only one who might want to display money columns in a tkinter treeview.
Anyway, if you have any suggestions for how I can accomplish this, please let me know.
ChatGPT suggested the following:
import tkinter as tk
import tkinter.ttk as ttk
# Create the main window
root = tk.Tk()
# Create a ttk.Treeview widget
tree = ttk.Treeview(root)
tree.pack()
# Insert some rows into the Treeview widget
tree.insert('', 'end', text='Row 1', values=('10.50', '20.00'))
tree.insert('', 'end', text='Row 2', values=('15.00', '25.00'))
tree.insert('', 'end', text='Row 3', values=('20.00', '30.00'))
# Define a formatting function for the cells
def format_currency(value):
return '${:,.2f}'.format(float(value))
# Set the formatting function for the cells in the second column
tree.tag_configure('currency', foreground='red',
font='Arial 14 bold', format=format_currency)
tree.tag_bind('currency', '<1>', lambda e: e.widget.item(e.item, tags=[]))
# Set the tag for the cells in the second column
for item in tree.get_children():
tree.item(item, tags=['currency'], text=1)
# Run the main loop
root.mainloop()
but there is no such parameter "format" for tree.tag_configure. The error I get when trying to run this code is: "TclError: unknown option "-format""
I was expecting the values in the second column to be formatted according to the format_currency function. I don't think ChatGPT quite got the format I want for the currency values, but at this point, it seems the priority is getting any format to be applied.
With Tkinter in Python, is it possible to format a column in a treeview control as currency with red font for negative values?
No, it is not possible. You can't control the color of individual cells in a treeview widget.
I have a python tkinter treeview that has a header font size of 12 and an row font size of 8. I have one item in the treeview. In one of the columns (lets say column 3) of the item, i have a text string that is significantly longer than the width of the column. If I use a word wrap function to warp the text string after 90 characters, the text string creates 22 rows of text in the item's column - all at font size 8.
How can I use style, height, and rowheight to set the treeview widget so that the full text in the column is displayed in the treeview and the bottom of the treeview aligns with the bottom of column 3 of the item - so that there's no white space between the bottom of the item and the bottom of the treeview? I've tried using metrics with my font to calculate the linespace and then set rowheight to linespace. But nothing seems to work.
header_font = tkfont.Font(family='TimesNewRoman', size=12)
row_font = tkfont.Font(family='TkFixedFont', size=8)
row_font.metrics()['linespace']
style = ttk.Style()
treeview.height=1
style.configure("Treeview.Heading", font=header_font, rowheight=int(12*2.5))
style.configure("Treeview", font=row_font, rowheight=(row_font.metrics()['linespace']*2.5))
Note: The treeview is in a frame using pack.
I can get the text to wordwrap using textwrap.wrap. For one item, this would result in 22 lines of text from the single string. For another item, this may result if 21 lines, etc. Note: I'm only displaying one item at a time in the treeview because of the length of the textstring. And I can set the font using a style to get it displayed in 8 point.
What I can't figure out is how to get the size of the treeview and/or frame to cut off at the bottom of the item/column so that I have no whitespace between the bottom of the item/column and the bottom of the treeview/frame, and the border around the item/columns is the bottom border of the treeview/frame.
If I set rowheight to the number of rows created from the textwrap, sometimes the bottom will cut off before the last row(s) for the item is displayed, sometimes in the middle of the row. And if I try to add a factor to the rowheight, it may be on for one row and off for other column heights.
Note: I will be loading textstrings of different length into the longest column so that's why I need to figure out a formula that will set the bottom of the treeview/frame to be the bottom of the content area/item area/column with the biggest height.
I have created a simple pandastable form in python, but I have some problems getting the rows in colors.
I have tried the following definition from the documentation, but it does not seem to work?
pt.setRowColors(rows=rows1, clr="red")
Here is my code:
# pandas as pt
# rows1 is a list of rows i would like to color
app = tk.Tk()
f = tk.Frame(app)
f.pack(fill=tk.BOTH,expand=1)
pt = Table(f, dataframe=myData, showtoolbar=False, showstatusbar=False)
pt.show()
pt.setRowColors(rows=rows1, clr="red")
pt.redraw()
I wanted 30 rows to have a red background, but it does nothing. I do not even get an error to go by...
Hope you can help.
The pandastable API docs suggest you should use a Hex value for the colour:
setRowColors(rows=None, clr=None, cols=None)[source]
Set rows color from menu. :param rows: row numbers to be colored :param clr: color in hex :param cols: column numbers, can also use ‘all’
So, according to this, the following should color all rows red in row1:
setRowColors(rows=rows1, clr='#FF0000', cols=None)
The columns of my QTableWidget do not fill in the space of the table, so that an empty space is left on the right hand-side. How to make the columns of my QTableWidget assume the maximum space so as to fill in this space?
The headers of the table have methods for controlling this:
header = table.horizontalHeader()
header.setStretchLastSection(True)
or:
header.setResizeMode(QHeaderView.Stretch)
I don't know of any method to set this property to the QTableWidget's content. However, I could use the following to get the columns to resize:
def resizeEvent(self, event):
self.setColumnWidth(0, event.size().width())
This resizes the first column only. To resize all columns, one should get all children item and apply column width / number of items.
I created a matrix of entry boxes using Tkinter and Python 2.3. What I want to do is to check the row of the entry box for a keyword and then insert a text into an entry box with the same row but of different column. For example, take a simple 3x3 entry box matrix:
Food Like Dislike
Apple Yes
Orange Yes
Let's say I have two lists that corresponds to each other as such:
1. [Apple, Orange]
2. [Dislike, Like]
I would like to check that if first element in list 1 is Apple and first element in list 2 is Dislike, then I want to insert a text "YES" into the entry box of 2nd row and 3rd column as shown in the above matrix.
I have appended the entry box indexes into two lists. First list contains the indices of the 1st column of entry boxes. Second list contains the 2nd and 3rd columns of the entry boxes. Problem is, how to determine the correspoding row and column of the entry box to insert the Yes???
Hope you can understand! This example is the most elementary one. My actual matrix consist of more rows and columns. An idea or sample code will help a lot.
Thank you Tkinter Gods.
Store references to the entry widgets in a dictionary of lists. Here's an example using the label as the key:
for item in list_of_things:
label = tk.Label(...)
column0 = tk.Entry(...)
column1 = tk.Entry(...)
matrix[label] = (column1, column2)
You can now get column1 of "Orange" with something like matrix["Orange"][1]