I encountered something I don't understand. I could assume it is a different measurement but I can't find any information about it.
I don't understand why I see that the width of the entry(I gave: 146 pixels) is bigger then windows width(I gave 420 pixels ) ? Increase the width of the window with the cursor and you will see that it is much wider than the window itself. Can you explain it ? How can this be controlled?
from tkinter import *
win = Tk()
win.geometry("465x420")
entry1 = Entry(win, background="white", width=146)
entry1.grid(row=1, column=1, ipadx=0)
win.mainloop()
CodePudding user response:
Try this, with the default font of Tkinter I divide the window
size by 8
and round it.
from tkinter import *
win = Tk()
win.geometry("465x420")
entry1 = Entry(win, background="white")
entry1.grid(row=1, column=1, ipadx=0)
win.bind("<Configure>", lambda e: entry1.configure(width=round(e.width/8)))
win.mainloop()
CodePudding user response:
tkinter has w.winfo_width
to find the width of a widget.
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
entry10 = tk.Entry( root, width = 10 )
entry20 = tk.Entry( root, width = 20 )
entry10.grid()
entry20.grid()
ttkentry10 = ttk.Entry( root, width = 10 )
ttkentry20 = ttk.Entry( root, width = 20 )
ttkentry10.grid()
ttkentry20.grid()
def on_click():
print( '10: ', entry10.winfo_width(), '20: ', entry20.winfo_width() )
print( 'ttk 10: ', ttkentry10.winfo_width(),
'ttk 20: ', ttkentry20.winfo_width() )
ttk.Button( root, text = 'Click', command = on_click ).grid()
root.mainloop()
Results from click:
10: 102 20: 192
ttk 10: 104 ttk20: 194
On my Mac this gives 9 pixels per character 12 pixels for a tk.Entry
and 9 pixels per character 14 pixels for a ttk.Entry
with the default font.