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?
Related
Let's say I have a message box in Python tkinter, for example:
from tkinter import messagebox
messagebox.showinfo("Example", "This is an example")
I want to be able to handle the message box's closing protocol. I know you can do it with tkinter windows, like so:
window.protocol("WM_DELETE_WINDOW", on_closing)
But my question is how do you do it with message boxes?
I figured out that what I wanted to do isn't possible (with the help of #Matiiss). So what I did instead was I created a toplevel widget, added buttons to it and handled its closing protocol. Example:
import tkinter as tk
from tkinter import messagebox
window = tk.Tk()
window.title("Example")
window.state("zoomed")
def protocol_handler():
# code
window2 = tk.Toplevel(window)
window2.geometry("300x220+500+200")
label1 = tk.Label(window2, text="Would you like to continue?")
label1.place(x=20, y=10)
button1 = tk.Button(window2, text="Yes")
button1.place(x=50, y=100)
button2 = tk.Button(window2, text="No")
button1.place(x=100, y=100)
window2.protocol("WM_DELETE_WINDOW", protocol_handler)
window.mainloop()
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:
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
I need to activate many entries when button is clicked
please do not write class based code, modify this code only because i need to change the whole code for the project as i did my whole project without classes
from Tkinter import *
import ttk
x='disabled'
def rakhi():
global x
x='NORMAL'
root = Tk()
frame=Frame(root)
frame.pack()
entry1=Entry(frame,state=x)
entry1.pack()
entry2=Entry(frame,state=x)
entry2.pack()
button1=Button(frame,text="press",command=rakhi)
button1.pack()
mainloop()
You need to use the configure method of each widget:
def rakhi():
entry1.configure(state="normal")
entry2.configure(state="normal")
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: