问题描述
我想检测用户是否点击了最大化"按钮:
I would like to detect if user has clicked the 'maximize' button:
当然是在 tkInter 中,但我不知道如何.
In tkInter of course, but I don't know how.
我曾尝试通过 StackOverflow、The Web & 进行搜索tkInter 文档(主要是 effbot 的 tkinterbook),但没有找到与我想要获取的内容相关的任何内容.
I have tried searching through StackOverflow, The Web & tkInter documents(mostly effbot's tkinterbook), but have not found anything related to what I am trying to get.
推荐答案
有一个使用 .bind()
的好方法,让我们开始吧!
There is a good way to does it using .bind()
, so let's get started!
我们知道,我们可以使用命令 .state('zoomed')
最大化窗口.
As we know, we can maximize the window using the command .state('zoomed')
.
root.state('zoomed')
而且我们可以通过 .bind("", my_function)
所以我们可以创建一个简单的函数来捕获最大化窗口事件,不一定是点击事件,但它可以工作.
So we can create create a simple function that catches a maximize window event, not necessarily a event by click, but it works.
这是一个例子:
import tkinter
def window_event(event):
if root.state() == 'zoomed':
print("My window is maximized")
if __name__ == '__main__':
root = tkinter.Tk()
root.title("Maximized")
root.bind("<Configure>", window_event)
root.mainloop()
编辑 1:新功能
import tkinter
def window_event(event):
if root.state() == 'zoomed':
print("My window is maximized")
#GET A NORMAL WINDOW EVENT
#You can easily do this by a conditional statement, but remember if you even move the window position,
#this conditional statement will be true, because move the window is a window event
if root.state() == 'normal':
print("My window is normal")
if __name__ == '__main__':
root = tkinter.Tk()
root.title("Window")
root.geometry("620x480")
root.bind("<Configure>", window_event)
root.mainloop()
编辑 2:新功能
import tkinter
count = 0
def window_event(event):
global count
if root.state() == 'zoomed':
print("My window is maximized")
count = 0
if root.state() == 'normal' and count < 1:
print("My window is normal")
count +=1
if __name__ == '__main__':
root = tkinter.Tk()
root.title("Window")
root.geometry("620x480")
root.bind("<Configure>", window_event)
root.mainloop()
看看这个链接,它们是另一种使用 Python GUI 的方法:
Take a look at this links, they are another way to work with Python GUI:
这篇关于检测用户是否点击了“最大化"按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!