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)
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()
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
Related
I have created a vocabulary journal by using Tkinter, and I am struggling to pop a window that i created already.
Since i am a beginner, I have not really used class everywhere..
project/build/gui.py
def open_new():# button command
print("clicked")
button_1 = Button(
image=button_image_1,
borderwidth=0,
highlightthickness=0,
command=open_new,
relief="flat"
)
I have created another window in another directory
project/build/build2/gui.py
from db import Database
from pathlib import Path
from tkinter import messagebox
from tkinter import Tk, Canvas, Entry, Text, Button, PhotoImage,Frame
ob=Database('store.db')
def add_values():
if entry_1.get()=='' or entry_2.get()=='':
messagebox.showerror('no value','please enter anu words')
return
ob.insert(entry_1.get(),entry_2.get())
# print(ob.fetch())
messagebox.showinfo("value has been added", "successfully added")
window = Tk()
blah blah
....
window.mainloop()
WhatI want to do is pop up the second module, which is actually a window. I do not think , it is possible to pop up this window by using toplevel(), is it?
So which code should i use to pop up it?
This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed last year.
I wanted to make button in tkinter, but when I started program, the command always calls when code just starts.
Here is example code:
import tkinter as tk
from tkinter import messagebox
window = tk.Tk()
window.title("Why this don't works???")
window.wm_geometry("100x100")
def message():
messagebox.showinfo("Hi there")
button = tk.Button(text="Hello", command=message())
button.grid(column=0, row=0)
while True:
window.update()
And then, button didn't worked. (When you press it, it don't works.)
I don't know what I'm doing wrong, so I need help.
The command should be a pointer to a function
In the code you wrote, the command gets the return value from the function.
command=message()
The correct way is
command = message
The problem is you are requesting a return value from the fucnction. Try using this.
from tkinter import *
# import messagebox from tkinter module
import tkinter.messagebox
# create a tkinter root window
root = tkinter.Tk()
# root window title and dimension
root.title("When you press a button the message will pop up")
root.geometry('75x50')
# Create a messagebox showinfo
def onClick():
tkinter.messagebox.showinfo("Hello World!.", "Hi I'm your message")
# Create a Button
button = Button(root, text="Click Me", command=onClick, height=5, width=10)
# Set the position of button on the top of window.
button.pack(side='top')
root.mainloop()
You have 2 errors:
first:
It must be command=message
second:
You must give a message argument too, you entered a title only.
Or, what you can do is.
Add another variable.
command = message()
Before this line,
button = tk.Button(text="Hello", command=message())
And chande this line to,
button = tk.Button(text="Hello", command=command)
I'm using tkinter's "askokcancel" message box to warn the user, with a pop-up, of an irreversible action.
from tkinter import Tk
Tk().withdraw()
from tkinter.messagebox import askokcancel
askokcancel("Warning", "This will delete stuff")
I'd like to change the text of the 'OK' button (from 'OK') to something like 'Delete', to make it less benign-looking.
Is this possible?
If not, what is another way to achieve it? Preferably without introducing any dependancies...
Why not open a child window thus creating your own box with your own button like this:
from tkinter import *
def messageWindow():
win = Toplevel()
win.title('warning')
message = "This will delete stuff"
Label(win, text=message).pack()
Button(win, text='Delete', command=win.destroy).pack()
root = Tk()
Button(root, text='Bring up Message', command=messageWindow).pack()
root.mainloop()
No, there is no way to change the text of the buttons for the built-in dialogs.
Your best option is to create your own dialog. It's not very hard to do, and it gives you absolute control over what is in the dialog widget.
It is true that you cannot change names in Tkinter messageboxes, but you can make your own class of messagebox so that you can reuse it several times. The following code is the same as Tkinter messagebox, but it has args and kwargs as arguments. It is just for convenience.
I am also learning, so the code does not have flash and alarm.
class Message(object):
def __init__(self,parent, *args, **kwargs):
self.parent=parent
top=Toplevel(self.parent)
top.geometry("400x200")
top.transient(self.parent)
top.title(args[0])
f0=Frame(top)
top.l1=Label(f0, text=args[1])
top.l1.pack(pady=30)
f0.pack()
top.grab_set()
top.f1=Frame(top)
for key in kwargs:
key=Button(top.f1, text=f"{kwargs[key]}")
key.pack(side=LEFT)
top.f1.pack()
The message box will look like this:
I want to launch an "Open File" dialog in Tkinter in Python 2.7.
My code starts with:
from Tkinter import Frame, Tk, BOTH, Text, Menu, END
import tkFileDialog as tkfd
import fileinput
root = Tk()
global strTab
strTab = ""
def openTab(event):
r = tkfd.askopenfilename()
strTab = unicodedata.normalize('NFKD', r).encode('ascii','ignore')
Later in the code I have:
btnLoadTab = Button(root,
text="Load Tab",
width=30,height=5,
bg="white",fg="black")
btnLoadTab.bind("<Button-1>", openTab)
btnLoadTab.pack()
root.mainloop()
When I press the button an "Open File" dialog is shown, but when I select a file it closes and the button remains "clicked".
If I later call to strTab outside of openTab, it remains equal to "".
You can find workable example here: http://www.python-course.eu/tkinter_dialogs.php
I'm using tkinter's "askokcancel" message box to warn the user, with a pop-up, of an irreversible action.
from tkinter import Tk
Tk().withdraw()
from tkinter.messagebox import askokcancel
askokcancel("Warning", "This will delete stuff")
I'd like to change the text of the 'OK' button (from 'OK') to something like 'Delete', to make it less benign-looking.
Is this possible?
If not, what is another way to achieve it? Preferably without introducing any dependancies...
Why not open a child window thus creating your own box with your own button like this:
from tkinter import *
def messageWindow():
win = Toplevel()
win.title('warning')
message = "This will delete stuff"
Label(win, text=message).pack()
Button(win, text='Delete', command=win.destroy).pack()
root = Tk()
Button(root, text='Bring up Message', command=messageWindow).pack()
root.mainloop()
No, there is no way to change the text of the buttons for the built-in dialogs.
Your best option is to create your own dialog. It's not very hard to do, and it gives you absolute control over what is in the dialog widget.
It is true that you cannot change names in Tkinter messageboxes, but you can make your own class of messagebox so that you can reuse it several times. The following code is the same as Tkinter messagebox, but it has args and kwargs as arguments. It is just for convenience.
I am also learning, so the code does not have flash and alarm.
class Message(object):
def __init__(self,parent, *args, **kwargs):
self.parent=parent
top=Toplevel(self.parent)
top.geometry("400x200")
top.transient(self.parent)
top.title(args[0])
f0=Frame(top)
top.l1=Label(f0, text=args[1])
top.l1.pack(pady=30)
f0.pack()
top.grab_set()
top.f1=Frame(top)
for key in kwargs:
key=Button(top.f1, text=f"{kwargs[key]}")
key.pack(side=LEFT)
top.f1.pack()
The message box will look like this: