简体   繁体   English

Python tkinter 如何放大小部件

[英]Python tkinter how to zoom in widgets

My code:我的代码:

import tkinter as tk

root = tk.Tk()

for i in range(50):
    for j in range(50):
        tk.Button(height=1, width=2, bg='Blue').grid(row=j, column=i)

root.mainloop()

I can not see all of the buttons in the screen even when I maxmize the window.即使最大化 window,我也看不到屏幕上的所有按钮。 so I want to add an option to zoom out (all of the widgets will be smaller) so I can see all of them.所以我想添加一个缩小选项(所有小部件都会变小),这样我就可以看到所有这些小部件。 How do I do that?我怎么做?

From what i have been able to understand from your question, i think you want the window to be resizable in tkinter.根据我从您的问题中了解到的情况,我认为您希望 window 在 tkinter 中可调整大小。

To allow a window to be resized by the user, we use the resizable function -:为了允许用户调整 window 的大小,我们使用可调整大小的 function -:

root.resizable(height = True, width = True)

In this case the two args are height and width which help you customize if you only want the widget to be vertically resizable or only want it to be horizontally resizable.在这种情况下,两个参数是高度和宽度,如果您只希望小部件垂直调整大小或只希望它水平调整大小,它们可以帮助您自定义。

Your code with this function added should look like this -:添加了此 function 的代码应如下所示:

import tkinter as tk

root = tk.Tk()
root.resizable(True, True) #Assuming you want both vertically and horizontally resizable window.

for i in range(50):
    for j in range(50):
        tk.Button(height=1, width=2, bg='Blue').grid(row=j, column=i)

root.mainloop()

I hope this will solve your problem.我希望这能解决你的问题。 And I also hope you are safe in this time of an ongoing pandemic.我也希望你在这个持续流行的大流行时期是安全的。

Example code:示例代码:

import tkinter as tk

root = tk.Tk()
root.state('zoomed')
widgets_to_zoom_list = []
DEFAULT_SIZE = 50


def zoom(widget):
    for every_widget in widgets_to_zoom_list:
        every_widget.config(width=widget.get(), height=widget.get())


def main():
    canvas = tk.Canvas(root)
    frame = tk.Frame(canvas)
    zoom_scale = tk.Scale(root, orient='vertical', from_=1, to=100)
    zoom_scale.config(command=lambda args: zoom(zoom_scale))

    zoom_scale.set(DEFAULT_SIZE)

    pixel = tk.PhotoImage(width=1, height=1)
    for i in range(50):
        btn = tk.Button(frame, text=str(i + 1), bg='Blue', image=pixel, width=DEFAULT_SIZE, height=DEFAULT_SIZE, compound="c")
        btn.grid(row=0, column=i)
        widgets_to_zoom_list.append(btn)

    canvas.create_window(0, 0, anchor='nw', window=frame)
    # make sure everything is displayed before configuring the scroll region
    canvas.update_idletasks()

    canvas.configure(scrollregion=canvas.bbox('all'))
    canvas.pack(fill='both', side='left', expand=True)
    zoom_scale.pack(fill='y', side='right')
    root.mainloop()


if __name__ == '__main__':
    main()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM