如何在Python中绑定到Property类型事件?

huangapple go评论82阅读模式
英文:

How do I bind to the Property type event in python?

问题

我正在尝试编写一个Python Tkinter程序,以探索如何制作响应式文本。在查阅某些Tkinter文章后,我决定rowconfigurecolumnconfigure是不够的,因为文本的字体大小也需要改变。因此,我决定绑定到一个事件,以便在窗口大小更改时,我可以查看窗口大小并决定文本字体,就像在Bootstrap JS中所做的那样。我的问题是,我只能找到一个解释如何绑定到窗口大小调整事件的文本,但它是为Tk/Tcl编写的,我不明白如何将其翻译成Python。

我可以从那个文件中看到,我需要做类似于以下的事情:

root.bind("<Property>", on_resize_do_something_function)

但我如何指定要监听的属性名称(大小)呢?页面末尾有一些提示,但我不明白如何将它们翻译成Python。确切地说,我如何在Python中绑定到Tk窗口的属性更改事件?我在上面提供了页面链接,以防被忽略。

我将感激您的帮助。提前感谢。

英文:

I am trying to write a python tkinter program to investigate how I can make a responsive text. After reviewing certain tkinter articles. I decided that rowconfigure and columnconfigure is just not enough, since the font size of the text will also need to change. So I decided to bind to an event, such that whenever the windows size is changed, I can look at the windows size and decide the text font like is done in bootstrap js. My problem is that there is only one text I can find that explains how I can bind to a window resize event, but it is written for Tk/Tcl and I do not understand how that translates to python.

I can see from that file that I will need to do something like

root.bind(&quot;&lt;Property&gt;&quot;, on_resize_do_something_function)

but how do I specify the property name (size) which I want to watch for. Various hints are around the end of the page, but I do not understand how to translate them to python. Precisely, how I can bind to a property change event on a tk window in python? I posted a link to the page above in case it went unnoticed.

I will appreciate your help. Thanks in advance.

答案1

得分: 1

如果您试图捕获对“root”窗口的更改,例如用户调整大小或移动窗口,您可以绑定到“&lt;Configure&gt;”事件,例如:

def on_resize(e):
    global last_size  # 允许此函数更新“last_size”值
    # 检查此事件是否由根窗口生成
    # 并检查窗口的大小是否与上一个事件不同
    if str(e.widget) == '.' and (e.width, e.height) != last_size:
        ...  # 做一些操作!
        last_size = (e.width, e.height)  # 使用新的根窗口大小更新

root.bind('<Configure>', on_resize)
# 获取根窗口的初始大小
last_size = (root.winfo_width, root.winfo_height)

请注意,此事件将在创建“root”窗口时立即触发,但这在大多数情况下不太可能引发问题。

传递的“Configure”事件对象具有以下属性:

  • event.x - 根窗口的x位置
  • event.y - 根窗口的y位置
  • event.width - 根窗口的宽度
  • event.height - 根窗口的高度
英文:

If you're trying to capture changes to the root window, such as the user resizing or moving the window, you can bind to the &#39;&lt;Configure&gt;&#39; event, e.g.:

def on_resize(e):
    global last_size  # allow this function to update the &#39;last_size&#39; value
    # check if this event was generated by the root window
    # and if the size of the window has changed from the last event
    if str(e.widget) == &#39;.&#39; and (e.width, e.height) != last_size:
        ...  # do something!
        last_size = (e.width, e.height)  # update with the new root window size


root.bind(&#39;&lt;Configure&gt;&#39;, on_resize)
# get the initial size of the root window
last_size = (root.winfo_width, root.winfo_height)

Note that this event will fire immediately when the root window is created, but that's not likely to cause issues in most cases

The Configure event object passed has the following properties:

  • event.x - the x position of the root window
  • event.y - the y position of the root window
  • event.width - the width of the root window
  • event.height - the height of the root window

huangapple
  • 本文由 发表于 2023年3月7日 01:03:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75653742.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定