假设我有一个任意大小的按钮小部件,
如果是这样的话是什么?
答案 0 :(得分:1)
不,没有任何内置功能。你可以使它工作,但是tkinter的设计是相反的:你指定文本,小部件会自动调整大小以适应。
答案 1 :(得分:1)
#! Python3
import tkinter as tk
from tkinter.font import Font
def resizeEvent(event, widget):
#widget.unbind("<Configure>")
#widget.grid_forget()
widgetFont = Font(font=widget['font'])
currentWidth = widget.winfo_width()
currentHeight = widget.winfo_height()
while ((widget.winfo_reqwidth() < currentWidth) and (widget.winfo_reqheight() < currentHeight)) and widgetFont['size'] > 1:
widgetFont['size'] += 1
widget['font'] = widgetFont
while ((widget.winfo_reqwidth() > currentWidth) or (widget.winfo_reqheight() > currentHeight)) and widgetFont['size'] > 1:
widgetFont['size'] -= 1
widget['font'] = widgetFont
widget['font'] = widgetFont
#widget.bind("<Configure>", lambda event, widget = widget : resizeEvent(event, widget))
def reqWidth(font, widget):
return font.measure(widget['text'])
def reqHeight(font, widget):
return font.metrics("linespace")
def font_resizable(widget, isResizable=True):
if isResizable:
widget.bind("<Configure>", lambda event, widget = widget : resizeEvent(event, widget))
else:
widget.unbind("<Configure>")
if __name__ == "__main__":
root = tk.Tk()
frame = tk.Frame(root)
frame.pack(fill="both", expand=True)
frame.grid_rowconfigure(0, weight=1, uniform=True)
frame.grid_rowconfigure(1, weight=1, uniform=True)
frame.grid_columnconfigure(0, weight=1, uniform=True)
button = tk.Entry(frame, text="A")
button.grid(row=0, column=0, rowspan=1, sticky="nsew")
button1 = tk.Button(frame, text="Dans et üstümde!")
button1.grid(row=1, column=0, rowspan=5, sticky="nsew")
font_resizable(button)
font_resizable(button1)
root.mainloop()
在某些例子中,它并不完全理想且行为不端,但是我想发布这个作为参考,或者至少从这一点开始。