0
votes

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

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)
2

2 Answers

1
votes

Yes, you can bind to control and alt characters. Bindings are fairly well documented. Here's one good source of information:

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

As an example, to bind to ctrl-alt-x you would do this:

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:

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()
0
votes

If you want something like: Press button A, then press button B then open a Message box it is possible.

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

Edit: Now a and b keyboard button insted of tkinter buttons