admin管理员组

文章数量:1405385

I am trying to display unicode characters in a Combobox using Tkinter. For this example, I don't fill a drop-down list, I only use the entry field. I already watched stack overflow topics:

  • how to use unicode and special chareacters characters in Tkinter
  • how to specify and view high-numbered unicode characters I did not find examples in relation with the following phenomenon:
  1. assigning the string 'hello' to the combo: (*) the button displays the wanted glyph tetragram the combobox displays 'Hello'

  2. assigning the same high-numbered unicode to the button and the combobox: (**) the button and the combobox display the wanted glyph tetragrams

  3. assigning a different high-numbered unicode to the button and the combobox: (***) the button displays the wanted glyph tetragram the combobox displays a glyph indicating an unknown symbol

'''

import tkinter as tk
from tkinter import *
from tkinter import ttk, font
import unicodedata

win = Tk()

win.geometry("270x700")

cbstyle = ttk.Style()
btstyle = ttk.Style()

btstyle.configure('styBut.TButton', font=('DejaVu Sans', 20))
cbstyle.configure('styComb.TCombobox', font=('DejaVu Sans', 20))

strVaButton = tk.StringVar()
strVaCombo = tk.StringVar()

strVaButton.set(u'\U0001D354') 
#strVaCombo.set(u'\U0001D354')     (**)
strVaCombo.set(u'\U0001D320')      (***)
#strVaCombo.set('Hello !')         (*)

print(strVaButton.get())
print(type(strVaButton.get()))
print(unicodedata.name(strVaButton.get()))
#print(unicodedata.name(strVaCombo.get()))   

theBut = ttk.Button(win, textvariable = strVaButton,style="styBut.TButton")
theComb = ttk.Combobox(win, textvariable = strVaCombo,style="styComb.TCombobox")


theBut.pack()
theComb.pack()

win.mainloop()'''

I am trying to display unicode characters in a Combobox using Tkinter. For this example, I don't fill a drop-down list, I only use the entry field. I already watched stack overflow topics:

  • how to use unicode and special chareacters characters in Tkinter
  • how to specify and view high-numbered unicode characters I did not find examples in relation with the following phenomenon:
  1. assigning the string 'hello' to the combo: (*) the button displays the wanted glyph tetragram the combobox displays 'Hello'

  2. assigning the same high-numbered unicode to the button and the combobox: (**) the button and the combobox display the wanted glyph tetragrams

  3. assigning a different high-numbered unicode to the button and the combobox: (***) the button displays the wanted glyph tetragram the combobox displays a glyph indicating an unknown symbol

'''

import tkinter as tk
from tkinter import *
from tkinter import ttk, font
import unicodedata

win = Tk()

win.geometry("270x700")

cbstyle = ttk.Style()
btstyle = ttk.Style()

btstyle.configure('styBut.TButton', font=('DejaVu Sans', 20))
cbstyle.configure('styComb.TCombobox', font=('DejaVu Sans', 20))

strVaButton = tk.StringVar()
strVaCombo = tk.StringVar()

strVaButton.set(u'\U0001D354') 
#strVaCombo.set(u'\U0001D354')     (**)
strVaCombo.set(u'\U0001D320')      (***)
#strVaCombo.set('Hello !')         (*)

print(strVaButton.get())
print(type(strVaButton.get()))
print(unicodedata.name(strVaButton.get()))
#print(unicodedata.name(strVaCombo.get()))   

theBut = ttk.Button(win, textvariable = strVaButton,style="styBut.TButton")
theComb = ttk.Combobox(win, textvariable = strVaCombo,style="styComb.TCombobox")


theBut.pack()
theComb.pack()

win.mainloop()'''
Share Improve this question asked Mar 22 at 9:35 JaHeJaHe 12 bronze badges 2
  • For some (unknown) reasons, tkinter combobox does not accept style configuration. What if you add win.option_add('*TCombobox*font', text_font) before theComb = ttk.Combobox(…) (providing text_font = ('DejaVu Sans', 20), of course)? – JosefZ Commented Mar 22 at 17:11
  • It works ! I don't think I would have found that 'win.option' so I had a look in the documentation though they say in 'tutorialspoint': "changing the default font will affect the font for all the widgets defined in the application" I have been able to successively display groups of combo with different fonts Thank you !! – JaHe Commented Mar 22 at 19:54
Add a comment  | 

1 Answer 1

Reset to default 0

win.option_add('*TCombobox*font', text_fontu) will affect the font for all the TCombobox widgets defined in the application. However, new TCombobox defaults could be overwritten for any particular TCombobox while defining it by ttk.Combobox:

import tkinter as tk
import tkinter.ttk as ttk
from tkinter import font

win = tk.Tk()
win.geometry("270x300")

text_fontb = ('DejaVu Sans', 20)
text_fontu = ('Unifont Upper', 20)
text_fontf = ('DejaVu Serif Condensed', 20)

style = ttk.Style()
style.configure('Stybut.TButton', font=text_fontb, width=12)

strVaButton = tk.StringVar(); strVaButton.set(u'\U0001D354 Sans')
strVaCombou = tk.StringVar(); strVaCombou.set(u'\U0001D320 Unifont')
strVaCombof = tk.StringVar(); strVaCombof.set(u'\U0001D320 Serif')

# set defaults for all `TCombobox` widgets
win.option_add('*TCombobox*font', text_fontu)
win.option_add('*TCombobox*width', 12)

theButon = ttk.Button  (win, textvariable=strVaButton, style="Stybut.TButton")
theCombu = ttk.Combobox(win, textvariable=strVaCombou)
theCombf = ttk.Combobox(win, textvariable=strVaCombof, font=text_fontf, width=8)

theButon.pack()
theCombu.pack()
theCombf.pack()

win.mainloop()

Explanation: in above code snippet,

  • theCombu = ttk.Combobox(win, textvariable=strVaCombou) defines theCombu Combobox using TCombobox default options declared in win.option_add, while
  • theCombf = ttk.Combobox(win, textvariable=strVaCombof, font=text_fontf, width=8) defines theCombf Combobox determining desired options (font and width in this case) explicitly.

Result:

本文标签: buttonUnexpected behaviour of tkinter combobox with highnumbered UnicodeStack Overflow