简体   繁体   English

python按钮组合以打开tkinter消息框

[英]python button combination to open a tkinter message box

im new topython 2.7 and want to know if it is possible to open a tkinter messagebox with a button combination on keyboard (Ctrl+alt+'something') that pops up like an windows error message 我是topython 2.7的新用户,想知道是否有可能使用像Windows错误消息那样弹出的键盘上的按钮组合(Ctrl + alt +'something')打开tkinter消息框。

import win32api
import time
import math
import Tkinter
import tkMessageBox

top = Tkinter.Tk()
def Message():
  tkMessageBox.showinfo("Window", "Text")

for i in range(9000):
  x = int(600+math.sin(math.pi*i/100)*500)
  y = int(500+math.cos(i)*100)
  win32api.SetCursorPos((x,y))
  time.sleep(.01)

Yes, you can bind to control and alt characters. 是的,您可以绑定到控制和alt字符。 Bindings are fairly well documented. 绑定已被很好地记录在案。 Here's one good source of information: 这是一个很好的信息来源:

http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm

As an example, to bind to ctrl-alt-x you would do this: 例如,要绑定到ctrl-alt-x,您可以这样做:

top.bind("<Control-Alt-x>", Message)

You can bind to a sequence of events by specifying the whole sequence. 您可以通过指定整个序列来绑定事件序列。 For example, if you wanted to implement a cheat code you could do something like this: 例如,如果您想实现作弊代码,则可以执行以下操作:

label.bind("<c><h><e><a><t>", Message)

For letters, "a" is the same as "<a>" , so you can also do this: 对于字母, "a""<a>"相同,因此您也可以这样做:

label.bind("cheat", Message)

Here is a complete working example: 这是一个完整的工作示例:

import Tkinter as tk
import tkMessageBox

def Message(event=None):
    tkMessageBox.showinfo("Window", "Text")

def Cheat(event=None):
    tkMessageBox.showinfo("Window", "Cheat Enabled!")


root = tk.Tk()
label = tk.Label(root, text="Press control-alt-m to see the messagebox\ntype 'cheat' to enable cheat.")
label.pack(fill="both", expand=True, padx=10, pady=100)

label.bind("<Control-Alt-x>", Message)
label.bind("<c><h><e><a><t>", Cheat)
label.focus_set()

root.mainloop()

If you want something like: Press button A, then press button B then open a Message box it is possible. 如果您想要以下内容:按下按钮A,然后按下按钮B,然后打开一个消息框,这是可能的。

Do something like: 做类似的事情:

from Tkinter import *
import tkMessageBox

def change():
    global switch
    switch=True

def new_window():
    if switch:
        tkMessageBox.showinfo("Random name", "Correct combination")
    else:
        print "Not the correct order"
root = Tk()
switch = False
root.bind("<A>", change)
root.bind("<B>",new_window)
root.mainloop()

If you want more buttons then use an integer and increase it while using switches for the correct button order. 如果您想要更多的按钮,则使用整数并在使用正确按钮顺序的开关时增加它。

Note that you can bind key combinations as well with root.bind("<Shift-E>") for example 请注意,例如,您也可以使用root.bind("<Shift-E>")绑定键组合。

Edit: Now a and b keyboard button insted of tkinter buttons 编辑:现在a和b键盘按钮由tkinter按钮安装

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

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