Python - Tkinter text size not resizing

ぐ巨炮叔叔 提交于 2019-12-11 07:18:24

问题


I am trying to make a window that resizes using Tkinter, and it works fine. But I want the font size to scale to proportion as well. The entry boxes resize perfectly but the text just stays the same size. Can I change the entry text size as well? How do I do This?

Thank you in Advance.

Here is a snippet of my code so far:

# defining home page
master = Tk()

master.iconbitmap('program_icon.ico')
master.geometry("410x280")
master.configure(background="#ff4f30")
master.title("Emailer : Log In | Sign Up")

for i in range(1, 10, 1):
    master.rowconfigure(i, weight=3)
for i in range(1, 5, 1):
    master.columnconfigure(i, weight=3)

master.columnconfigure(1, weight=50)
master.rowconfigure(2, weight=50)
master.rowconfigure(3, weight=50)
master.rowconfigure(6, weight=50)
master.rowconfigure(7, weight=50)

# adding text
l1 = Label(master, text="Welcome to the Emailer \n Sign up or Log in \n Note: Your password is protected by hash encryption", bg="#ff4f30").grid(row=0, column=1, sticky=N+S+W+E)
l2 = Label(master, text="Log In", bg="#ff4f30").grid(row=1, column=1, sticky=N+S+W+E)
l3 = Label(master, text="Email", bg="#ff4f30").grid(row=2, sticky=N+S+W+E)
l4 = Label(master, text="Password", bg="#ff4f30").grid(row=3, sticky=N+S+W+E)

l5 = Label(master, text="Sign Up", bg="#ff4f30").grid(row=5, column=1, sticky=N+S+W+E)
l6 = Label(master, text="Email", bg="#ff4f30").grid(row=6, sticky=N+S+W+E)
l7 = Label(master, text="Password", bg="#ff4f30").grid(row=7, sticky=N+S+W+E)

# adding top menu
menubar = Menu(master)
menubar.add_command(label="Help", command=helpuser)
menubar.add_command(label="Quit", command=master.quit)
master.config(menu=menubar)

# defining user input boxes
e1 = Entry(master)
e2 = Entry(master)
e3 = Entry(master)
e4 = Entry(master)

e1.grid(row=2, column=1, sticky=N+S+W+E)
e2.grid(row=3, column=1, sticky=N+S+W+E)
e3.grid(row=6, column=1, sticky=N+S+W+E)
e4.grid(row=7, column=1, sticky=N+S+W+E)

# setting e2 and e4 to only display '*' character
e2.config(show="*")
e4.config(show="*")

# defining buttons
Button(master, text='Sign Up', command=sign_up).grid(row=8, column=2, sticky=W + E, pady=4)
Button(master, text='Log In', command=log_in).grid(row=4, column=2, sticky=W + E, pady=4)

# starting window
mainloop( )

回答1:


maybe you can use a font object and develop a function bound to the window being resized ?

import tkinter as tk
import tkinter.font as tkFont


master = tk.Tk()
my_font = tkFont.Font(size=10)
def resizer(event):
   if event.width in range(300,325):
      my_font.configure(size=10)   
   elif event.width in range(400,425):
      my_font.configure(size=20)
   elif event.width > 600:
      my_font.configure(size=30)

a_label= tk.Label(font=my_font, text="Welcome")
a_label.grid()
an_entry = tk.Entry(font=my_font)
an_entry.grid()
an_entry.insert(0,'some text')
master.bind("<Configure>", resizer)
tk.mainloop()


来源:https://stackoverflow.com/questions/48733923/python-tkinter-text-size-not-resizing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!