Why Does "mainloop()" For One Window Open Multiple Windows? - python

I'm using Tkinter. For whatever reason, "Window1.mainloop()" in this script opens both Window1 and Window2. Also, pressing the button on Window1 doesn't open Window2, but I think that's unrelated.
import tkinter as tk
Window1 = tk.Tk()
Window1.title("Window 1")
Window1.geometry("300x200")
Window2 = tk.Tk()
Window2.title("Window 2")
Window2.geometry("150x100")
def RunWindow2():
Window2.mainloop()
Button1 = tk.Button(Window1, text = "Open Window 2.", command = RunWindow2)
Button1.pack()
Label1 = tk.Label(Window2, text = "This is Window 2.")
Label1.pack()
Window1.mainloop()

Related

Tkinter messagedialog askyesno with a checkbox?

Is it possible to make a Tkinter yes/no messagebox with a checkbox, for something like 'Never ask me again'?
Or would I have to create another window, create my own Labels and CheckButtons, and basically create my own dialog?
You should create your own dialog box then.
Here’s what you can do:
from tkinter import *
def popup():
popupWin = Toplevel()
popupWin.title(“Continue?”)
checkVariable = IntVar()
lbl = Label(popupWin, text=“Continue?)
lbl.pack()
btn2 = Button(popupWin, text=“Yes”)
btn2.pack()
btn3 = Button(popupWin, text=“No”)
btn3.pack()
checkBox = Checkbutton(popupWin, text=“Don’t ask again”, variable=checkVariable)
root = Tk()
btn = Button(root, text=Message Box, command=popup)
root.mainloop()

How to display text on a new window Tkinter?

I've started learning Tkinter on Python few weeks ago and wanted to create a Guess Game. But unfortunately I ran into a problem, with this code the text for the rules ( in the function Rules; text='Here are the rules... ) doesn't appear on the window ( rule_window).
window = Tk()
window.title("Guessing Game")
welcome = Label(window,text="Welcome To The Guessing Game!",background="black",foreground="white")
welcome.grid(row=0,column=0,columnspan=3)
def Rules():
rule_window = Tk()
rule_window = rule_window.title("The Rules")
the_rules = Label(rule_window, text='Here are the rules...', foreground="black")
the_rules.grid(row=0,column=0,columnspan=3)
rule_window.mainloop()
rules = Button(window,text="Rules",command=Rules)
rules.grid(row=1,column=0,columnspan=1)
window.mainloop()
Does anyone know how to solve this problem?
In your code you reset whatever rule_window is to a string (in this line: rule_window = rule_window.title(...))
Try this:
from import tkinter *
window = Tk()
window.title("Guessing Game")
welcome = Label(window, text="Welcome To The Guessing Game!", background="black", foreground="white")
welcome.grid(row=0, column=0, columnspan=3)
def Rules():
rule_window = Toplevel(window)
rule_window.title("The Rules")
the_rules = Label(rule_window, text="Here are the rules...", foreground="black")
the_rules.grid(row=0, column=0, columnspan=3)
rules = Button(window, text="Rules", command=Rules)
rules.grid(row=1, column=0, columnspan=1)
window.mainloop()
When you want to have 2 windows that are responsive at the same time you can use tkinter.Toplevel().
In your code, you have initialized the new instances of the Tkinter frame so, instead of you can create a top-level Window. What TopLevel Window does, generally creates a popup window kind of thing in the application. You can also trigger the Tkinter button to open the new window.
from tkinter import *
from tkinter import ttk
#Create an instance of tkinter window
root= Tk()
root.geometry("600x450")
#Define a function
def open_new():
#Create a TopLevel window
new_win= Toplevel(root)
new_win.title("My New Window")
#Set the geometry
new_win.geometry("600x250")
Label(new_win, text="Hello There!",font=('Georgia 15 bold')).pack(pady=30)
#Create a Button in main Window
btn= ttk.Button(root, text="New Window",command=open_new)
btn.pack()
root.mainloop()

My button doesn't apear after the initial popup - Python Message Box

I'm trying to make a little pop-up interface before my game to choose difficulties. The problem I'm having is that after I click yes on the first messagebox.askyesno() the whole pop up disappears and I have no idea on how to know which button was pressed to return an output. This is my first time using tkinter and messagebox so any help is greatly appreciated.
This is my code:
import tkinter as tk
from tkinter import messagebox
def start_game():
root = tk.Tk()
root.attributes("-topmost", True)
root.withdraw()
if messagebox.askyesno("Welcome to Snake!", "Would you like to play?") == True:
mainLabel = tk.Label(root, text='Choose a dificulty:')
easy_ask = tk.Button(root, text='Easy')
medium_ask = tk.Button(root, text='Medium')
hard_ask = tk.Button(root, text='Hard')
mainLabel.pack(side=tk.LEFT)
easy_ask.pack(side=tk.LEFT)
medium_ask.pack(side=tk.LEFT)
hard_ask.pack(side=tk.LEFT)
root.deiconify()
root.destroy()
root.quit()
root.mainloop()
start_game()
What about an else statement ;-)? Consider what your code is doing: if you click 'Yes', it creates some Button on a main window that you have withdrawn. Then, immediately after you deiconify it, you destroy it. Reshuffle your code like follows:
import tkinter as tk
from tkinter import messagebox
def start_game():
root = tk.Tk()
root.attributes("-topmost", True)
root.withdraw()
if messagebox.askyesno("Welcome to Snake!", "Would you like to play?") == True:
root.deiconify() # <--- deiconify under the True condition
mainLabel = tk.Label(root, text='Choose a dificulty:')
easy_ask = tk.Button(root, text='Easy')
medium_ask = tk.Button(root, text='Medium')
hard_ask = tk.Button(root, text='Hard')
mainLabel.pack(side=tk.LEFT)
easy_ask.pack(side=tk.LEFT)
medium_ask.pack(side=tk.LEFT)
hard_ask.pack(side=tk.LEFT)
else:
root.destroy() # <--- destroy and quit under the False condition
root.quit()
root.mainloop()
start_game()
Note that you can also assign the outcome of messagebox.askyesno to a variable:
answer = messagebox.askyesno("Welcome to Snake!", "Would you like to play?")

storing input from text box tkinter

import tkinter
window = tkinter.Tk()
window.configure(background="grey90")
window.title("Downloader")
window.geometry("300x300")
window.resizable(False, False)
entry = tkinter.Entry(window)
entry.place(x=70,y=68)
entry.configure(highlightbackground="grey90")
button = tkinter.Button(window, text="Download",
command=window.destroy, highlightbackground="grey90")
button.place(x=110,y=120)
to_download = entry.get()
window.mainloop()
print(to_download)
Hello, I need some help in storing the output after the "download" button is clicked,what I want is when "download" button is clicked the value to be stored to_download (if an input is placed not None) and the window to close.
Right now the window is closing but it doesn't store the value
Hi you should set a function to command and do this in function
import tkinter
def press():
global to_download
if to_download:
print(to_download)
window.destroy()
window = tkinter.Tk()
window.configure(background="grey90")
window.title("Downloader")
window.geometry("300x300")
window.resizable(False, False)
entry = tkinter.Entry(window)
entry.place(x=70,y=68)
entry.configure(highlightbackground="grey90")
button = tkinter.Button(window, text="Download",
command=press, highlightbackground="grey90")
button.place(x=110,y=120)
window.mainloop()
This line was probably accidentally omitted from the original code. It should be inserted in the press function before the if statement:
to_download = entry.get()
Here is the complete code:
import tkinter
def press():
global to_download
to_download = entry.get()
if to_download:
print(to_download)
window.destroy()
window = tkinter.Tk()
window.configure(background="grey90")
window.title("Downloader")
window.geometry("300x300")
window.resizable(False, False)
entry = tkinter.Entry(window)
entry.place(x=70,y=68)
entry.configure(highlightbackground="grey90")
button = tkinter.Button(window,
text="Download",
command=press,
highlightbackground="grey90")
button.place(x=110,y=120)
window.mainloop()

How to compulsory close message box for Toplevel window

image for that
I have few lines of code here which is login system which works fine but i can click on the Toplevel button multiple times when i provide the wrong password without closing the messagebox.How can i make it so that it has to be closed messagebox before i can make attempt again.
from tkinter import *
from tkinter import messagebox
def top():
if entry1.get() == "333":
log.destroy()
root.deiconify()
else:
messagebox.showerror("error", "try again")
root = Tk()
root.geometry("300x300")
log = Toplevel(root)
log.geometry("200x200")
label1 = Label(log, text="password")
entry1 = Entry(log)
button1 = Button(log, text="login", command=top)
label1.pack()
entry1.pack()
button1.pack(side="bottom")
lab = Label(root, text="welcome bro").pack()
root.withdraw()
root.mainloop()
You need to make the log window the parent of the dialog:
messagebox.showerror("error", "try again", parent=log)
By default it will use the root window (the Tk instance) as the parent which in this case is not what you want.
With hint from #furas this how to implement this:
create another function to the call it when the entry doesn't match and use grab_set method for the Toplevel window tp.grab_set().You can add your customarised image to the Toplevel window as well as message to display in the box(here: i use label to depict that)
from tkinter import *
from tkinter import messagebox
def dialog(): # this function to call when entry doesn't match
tp = Toplevel(log)
tp.geometry("300x100")
tp.title('error')
tp.grab_set() # to bring the focus to the window for you to close it
tp.resizable(width=False, height=False)
l = Label(tp, text="try again\n\n\n\n add your customarize image to the window")
l.pack()
def top():
if entry1.get() == "333":
log.destroy()
root.deiconify()
else:
dialog() # being called here
root = Tk()
root.geometry("300x300")
log = Toplevel(root)
log.geometry("200x200")
label1 = Label(log, text="password")
entry1 = Entry(log)
button1 = Button(log, text="login", command=top)
label1.pack()
entry1.pack()
button1.pack(side="bottom")
lab = Label(root, text="welcome bro").pack()
root.withdraw()
root.mainloop()

Categories