我正在尝试创建一个使用Tkinter调整大小的窗口,它工作正常。但我希望字体大小也按比例缩放。输入框完美调整大小,但文本大小保持不变。我也可以更改输入文字大小吗?我该怎么做?
先谢谢你。
到目前为止,这是我的代码片段:
# 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( )
答案 0 :(得分:2)
也许您可以使用字体对象并开发功能 绑定到正在调整大小的窗口?
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()