Tkinter window 字体在拉伸 window 时动态扩展

Tkinter window font to expand dynamically while stretching the window

我创建了一个 Tkinter GUI,我想在 window 扩展时扩展字体。到目前为止,我已经尝试过权重,但只有 window 扩展而不是字母。以下脚本是我尝试创建的代码示例。

from tkinter import ttk
import tkinter as tk
from tkinter.scrolledtext import ScrolledText
from tkinter import*
from tkinter.ttk import *


master = tk.Tk()
#Title of GUI
master.title("Test Name")
#Seperate first GUI tab in sections

stepOne = tk.LabelFrame(master, text=" 1. General Heatmaps: ")
stepOne.grid(row=0, columnspan=7, sticky='WE', \
             padx=10, pady=10, ipadx=10, ipady=10)

master.rowconfigure(1, weight=1)
master.columnconfigure(1, weight=1)

# this wil create a label widget 
l1 = Label(stepOne, text = "Input tilt:") 
l2 = Label(stepOne, text = "Input wind speed:") 
l3=  Label(stepOne, text = "Input path:") 
# grid method to arrange labels in respective 
# rows and columns as specified 
l1.grid(row = 1, column = 0, sticky = E, pady = 2) 
l2.grid(row = 2, column = 0, sticky = E, pady = 2) 
l3.grid(row = 3, column = 0, sticky = E, pady = 2)

entryTilt = Entry(stepOne)
entryWind = Entry(stepOne)
entryFolder_path=Entry(stepOne)

entryTilt .grid(row = 1, column = 1, pady = 2) 
entryWind .grid(row = 2, column = 1, pady = 2) 
entryFolder_path .grid(row = 3, column = 1, pady = 2)  

b1 = Button(stepOne, text = "Run script") 

b1.grid(row = 2, column = 2, sticky = E) 

master.mainloop()

试试这个:

from tkinter import ttk
import tkinter as tk
from tkinter.scrolledtext import ScrolledText
from tkinter import *
# from tkinter.ttk import *

wh_font_map = {k: v for k, v in zip(range(200, 1900, 50), range(10, 44))}
# {
#   200: 10,
#   250: 11,
#   300: 12,
#   350: 13,
#   400: 14,
#   450: 15,
#   500: 16,
#   550: 17,
#   600: 18,
#   650: 19,
#   700: 20,
#   750: 21,
#   800: 22,
#   850: 23,
#   900: 24,
#   950: 25,
#   1000: 26,
#   1050: 27,
#   1100: 28,
#   1150: 29,
#   1200: 30,
#   1250: 31,
#   1300: 32,
#   1350: 33,
#   1400: 34,
#   1450: 35,
#   1500: 36,
#   1550: 37,
#   1600: 38,
#   1650: 39,
#   1700: 40,
#   1750: 41,
#   1800: 42,
#   1850: 43
# }
def change_font(event):
    font_size = wh_font_map[next(k for k in wh_font_map if (master.winfo_width() + master.winfo_height()) / 2 <= k)]
    for widget in stepOne.winfo_children():
        widget['font'] = ("", font_size)


master = tk.Tk()
# Title of GUI
master.title("Test Name")
# Seperate first GUI tab in sections

stepOne = tk.LabelFrame(master, text=" 1. General Heatmaps: ")
stepOne.grid(row=0, columnspan=7, sticky='WE', \
             padx=10, pady=10, ipadx=10, ipady=10)

# master.rowconfigure(0, weight=1)
# master.columnconfigure(0, weight=1)

# this wil create a label widget
l1 = Label(stepOne, text="Input tilt:")
l2 = Label(stepOne, text="Input wind speed:")
l3 = Label(stepOne, text="Input path:")
# grid method to arrange labels in respective
# rows and columns as specified
l1.grid(row=1, column=0, sticky=E, pady=2)
l2.grid(row=2, column=0, sticky=E, pady=2)
l3.grid(row=3, column=0, sticky=E, pady=2)

entryTilt = Entry(stepOne)
entryWind = Entry(stepOne)
entryFolder_path = Entry(stepOne)

entryTilt.grid(row=1, column=1, pady=2)
entryWind.grid(row=2, column=1, pady=2)
entryFolder_path.grid(row=3, column=1, pady=2)

b1 = Button(stepOne, text="Run script")

b1.grid(row=2, column=2, sticky=E)
master.bind("<Configure>", change_font)
master.mainloop()


只是一个想法。

我的想法:

  1. 为window绑定事件。
  2. 获取字典 window.Use 的高度和宽度的平均值 wh_font_map 以获得字体大小。(您可以更改范围)

wh_font_map会生成一个average-font sizemapper.You可以手动更改。


设置字号ttk.Button需要用到ttk.Style()。 为了方便,我改成了tk.Button。如果真的需要用ttk.Button()。阅读一下ttk-Style