Tkinter 窗口字体在拉伸窗口时动态扩展

Posted

技术标签:

【中文标题】Tkinter 窗口字体在拉伸窗口时动态扩展【英文标题】:Tkinter window font to expand dynamically while stretching the window 【发布时间】:2020-09-11 19:36:29 【问题描述】:

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

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()

【问题讨论】:

改字体大小而不是布局,对吧? 是的,这是我的目标 您要更改所有内容的字体大小,还是只更改某些小部件? 字体大小是原来的目标,但我想看看如果我改变一切会是什么样子 【参考方案1】:

试试这个:

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()


只是一个想法。

我的想法:

    为窗口绑定一个事件。 获取窗口高度和宽度的平均值。使用字典wh_font_map获取字体大小。(您可以更改范围)

wh_font_map 将生成一个average-font sizemapper。您可以手动更改它。


设置ttk.Button的字体大小需要使用ttk.Style()。 为了方便,我把它改成tk.Button。如果你真的需要使用ttk.Button()。阅读ttk-Style

【讨论】:

以上是关于Tkinter 窗口字体在拉伸窗口时动态扩展的主要内容,如果未能解决你的问题,请参考以下文章

过分了,别人用来做桌面应用开发,这家伙却用来撩妹-上帝给你开了各种撩妹窗口(Tkinter)

过分了,别人用来做桌面应用开发,这家伙却用来撩妹-上帝给你开了各种撩妹窗口(Tkinter)

用Tkinter打造GUI开发工具(49)在Tkinter窗口上动态显示matplotlib.pyplot图形

用Tkinter打造GUI开发工具(49)在Tkinter窗口上动态显示matplotlib.pyplot图形

易语言怎么在选择夹里动态创建组件

如何在 tkinter 中创建一个自调整大小的按钮网格?