Change the width of the dropdown list from the ttk dropdown list

I am trying to change ttk combobox popup list width. Setting the width Combobox also changes the width of the list, making some of the values ​​unreadable.

I read this solution in Tk / Tcl, but I am not familiar with this language and would like to solve the problem with Python. I tried changing the theme settings, but it doesn't seem to help. Below is an example code snippet.

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
root.title("testing the combobox")
root.geometry('300x300+50+50')
fruit = ['apples are the best', 'bananas are better']

c = ttk.Combobox(root, values=fruit, width=10)
c.pack()

# Trying to change the width, does not work
c.option_add("*TCombobox*Listbox*Width", 50)

root.mainloop()

Anyone who can help me or give me some pointers?

+4
source share
2 answers

Tk, (ttk/combobox.tcl), combobox , ( ttk::combobox::PlacePopdown). , , -postoffset. , , , . : , , , .

Tcl/Tk : ttk::style configure TCombobox -postoffset {0 0 300 0} 300 ( x y).

UPDATE

tkinter python <Configure>, combobox postoffset, . : modified width combobox screenshot

import tkinter as tk
import tkinter.ttk as ttk
import tkinter.font as tkfont

def on_combo_configure(event):
    global fruit
    font = tkfont.nametofont(str(event.widget.cget('font')))
    width = font.measure(fruit[0] + "0") - event.width
    style = ttk.Style()
    style.configure('TCombobox', postoffset=(0,0,width,0))

root = tk.Tk()
root.title("testing the combobox")
root.geometry('300x300+50+50')
fruit = ['apples are the best', 'bananas are better']

c = ttk.Combobox(root, values=fruit, width=10)
c.bind('<Configure>', on_combo_configure)
c.pack()

root.mainloop()

, , , .

0

patthoyts TCombobox ( Tk, ).

, combobox ( , - , , ). , combobox , : , , , , .

import tkinter as tk
import tkinter.ttk as ttk
import tkinter.font as tkfont

def on_combo_configure(event):
    combo = event.widget
    style = ttk.Style()
    # check if the combobox already has the "postoffest" property
    current_combo_style = combo.cget('style') or "TCombobox"
    if len(style.lookup(current_combo_style, 'postoffset'))>0:
        return
    combo_values = combo.cget('values')
    if len(combo_values) == 0:
        return
    longest_value = max(combo_values, key=len)
    font = tkfont.nametofont(str(combo.cget('font')))
    width = font.measure(longest_value + "0") - event.width
    if (width<0):
        # no need to make the popdown smaller
        return
    # create an unique style name using widget id
    unique_name='Combobox{}'.format(combo.winfo_id())
    # the new style must inherit from curret widget style (unless it our custom style!) 
    if unique_name in current_combo_style:
        style_name = current_combo_style 
    else:
        style_name = "{}.{}".format(unique_name, current_combo_style)

    style.configure(style_name, postoffset=(0,0,width,0))
    combo.configure(style=style_name)

root = tk.Tk()
root.title("testing the combobox")
root.geometry('300x300+50+50')
fruit = ['apples are the best', 'bananas are way more better']

c = ttk.Combobox(root, values=fruit, width=10)
c.bind('<Configure>', on_combo_configure)
c.pack()

c1 = ttk.Combobox(root, values=['shorter','than','widget'], width=15)
c1.bind('<Configure>', on_combo_configure)
c1.pack()

root.mainloop()

...

, Tk Combobox : postoffest TCombobox, .

, [python-install-dir]\tcl\tk[version]\ttk\combobox.tcl; PlacePopdown:

set postoffset [ttk::style lookup TCombobox -postoffset {} {0 0 0 0}]

:

set style [$cb cget -style]
set postoffset [ttk::style lookup $style -postoffset {} {0 0 0 0}]

, , .

0

Source: https://habr.com/ru/post/1657047/


All Articles