How do I create a new tkinter root window on top of the main root window that prevents you from accessing the main window until you close the secondary window?
Here is the code that I've written so far (in python 3.6):
from tkinter import *
root = Tk()
root.geometry('600x400+0+0')
def new_page():
rootB = Tk()
btnNP = Button(root, padx=1, pady=2, fg='black',relief='raise',font=
('garamond',10, 'italic', 'bold'),
text='New Page', bg='blue', command=new_page)
btnNP.place(x=100, y=300)
text1 = Text(root, bd=5, height=1, width=14, bg='pink')
text1.place(x=100, y=250)
root.mainloop()
I use Toplevel instead of a new root
def New_page():
popup = Toplevel()
popup.grab_set()
this should do the trick
Related
I am making a text base game and would like to get a new root window on the click of a button while simultaneously closing the original one?
#Packages
import tkinter as tk
from tkinter import ttk, BOTTOM
from PIL import ImageTk, Image
#Create Window
root = tk.Tk()
root.geometry("360x740")
root.resizable(True, True)
root.title("Box")
root.configure(bg="black")
#Load Image
canvas = tk.Canvas(root, width=360, height=360, bg="black")
tk.Canvas(bg="black")
canvas.pack()
img = ImageTk.PhotoImage(Image.open("sample_image.jpg"))
canvas.create_image(180, 200, image=img)
#Create Text Widget
T = tk.Text(root, height=10, width=52, bg="black", fg="white")
l = tk.Label(root, text="Hard Living by Amsha", fg="white", bg="black")
l.config(font=(None, 14,))
story = """
"""
l.pack()
T.pack()
#Continue Button
yes_button = ttk.Button(root, text="Continue", command=lambda: next_window())
yes_button.pack(pady=75, side=BOTTOM)
T.insert(tk.END, story)
root.mainloop()
def next_window():
root.quit()
root = tk.Tk()
root.geometry("360x740")
root.resizable(True, True)
root.title("Hard Living")
root.configure(bg="black")
root.mainloop()
I have this small python tkinter project that basically does two things:
It receives data from the tkinter text widget and enters it into the tkinter Radiobutton widget in another window. Everything is working just fine except one issue.
It treats everything I enter as one and create only one Radiobutton. I want it to create a new Radiobutton anytime I move to a new line
Here is my code:
from tkinter import*
import re
import tkinter.scrolledtext as src
root=Tk()
root.geometry("400x400")
root.title("Still trying hard!!")
opt=StringVar()
db=opt.get()
var=StringVar()
var.get()
Txt_Cont=""
cont_formater=""
#FUNCTIONS
def _Text_Input(a):
if isinstance(db,str):
global Txt_Cont
root1 = Toplevel()
root1.geometry("400x400+500+200")
root1.title("DB AND TB WINDOWS")
cont=Txt_Cont
Radiobtn=Checkbutton(root1,text=cont, variable=var)
Radiobtn.deselect()
Radiobtn.pack()
root1.mainloop()
def btnfcn():
global Txt_Cont
Txt_Cont = text_box.get("1.0", "end-1c")
text_box = src.ScrolledText(root, width=40, height=10, bd=10, font=('arial', 10, 'bold'), padx=5,
pady=5)
text_box.pack()
btn = Button(root, text="click me", font=('arial', 10, 'bold'), command=btnfcn)
btn.pack(ipadx=50, pady=(10, 10))
Options = OptionMenu(root, opt, "Databases", "Tables", command=_Text_Input)
Options.pack(ipadx=60)
root.mainloop()
I'm trying to create a GUI that opens up a new window after pressing a button while destroying the last window. I'm not getting any errors but when I run my program nothing comes up.
from Tkinter import *
def team():
def table():
table = Toplevel(contributers)
contributers.destroy()
def contributers():
contributers = Toplevel(table)
table.destroy()
def firstpage():
firstpage = Toplevel(letsbegin)
letsbegin.destroy()
def secondpage():
secondpage = Toplevel(firstpage)
firstpage.destroy()
def leave():
exit()
root = Tk()
root.title("Team Blue")
label1 = label(menu, text="Team Blue", bg = "Yellow", fg="Black")
button1 = button(menu, text="ENTER", width=15, bg="yellow", fg="Black", command =contributers)
button2 = button(menu, text="Exit", bg="red", fg="white", command=leave)
root.mainloop()
I just want this code to run
You have many mistakes which I mentioned in comments.
If you want to close one window and open new one then destroy first window - root.destroy() - and later use again Tk() to create new window and use again mainloop().
I assign new window to global variable root so I can use almost the same code to close second window and open third one.
I use global root so variable root is not local variable but it is global and I have access (to window assigned to root) in other functions.
from Tkinter import *
# --- functions ---
def open_first_window():
global root
root = Tk()
label1 = Label(root, text="Team Brake 'Em")
label1.pack()
button1 = Button(root, text="Open Second Window", command=open_second_window)
button1.pack()
button2 = Button(root, text="Exit", command=root.destroy)
button2.pack()
root.mainloop()
def open_second_window():
global root
root.destroy()
root = Tk()
label1 = Label(root, text="Second Window")
label1.pack()
button1 = Button(root, text="Open Third Window", command=open_third_window)
button1.pack()
button2 = Button(root, text="Exit", command=root.destroy)
button2.pack()
root.mainloop()
def open_third_window():
global root
root.destroy()
root = Tk()
label1 = Label(root, text="Third Window")
label1.pack()
button2 = Button(root, text="Exit", command=root.destroy)
button2.pack()
root.mainloop()
# --- main ---
open_first_window()
There is other popular method - don't destry window but remove all widgets and put new one. Widget Frame can be usful because you can put all widget in Frame and Frame put in Window and later you have to only remove Frame and put new Frame with new widgets.
from Tkinter import *
# --- function ---
def create_first_frame():
global root
global frame
#frame.destroy()
frame = Frame()
frame.pack()
label1 = Label(frame, text="Team Brake 'Em")
label1.pack()
button1 = Button(frame, text="Open Second Window", command=create_second_frame)
button1.pack()
button2 = Button(frame, text="Exit", command=root.destroy)
button2.pack()
def create_second_frame():
global root
global frame
frame.destroy()
frame = Frame()
frame.pack()
label1 = Label(frame, text="Second Window")
label1.pack()
button1 = Button(frame, text="Open Third Window", command=create_third_frame)
button1.pack()
button2 = Button(frame, text="Exit", command=root.destroy)
button2.pack()
def create_third_frame():
global root
global frame
frame.destroy()
frame = Frame()
frame.pack()
label1 = Label(frame, text="Third Window")
label1.pack()
button2 = Button(frame, text="Exit", command=root.destroy)
button2.pack()
# --- main ---
root = Tk()
create_first_frame()
root.mainloop()
this is because as you wrapped your whole code inside the fuction name team().
so, you have to call that method at appropriate position in order to run the program.
and please make sure the letter case as label fuction is written as Label() so does button() is Button().
and also you have to use root in place of menu, then hopefully you see window.
pack the content according.
I am trying to create a Scrollbar for my text widget however, I cannot seem to be able to grid() the scrollbar, thus the scrollbar does not appear on the text widget. Ignore what is in the variable Quote, it is just test data.
EventScrollBar= tk.Scrollbar(EventChoice)
EventText=tk.Text(EventChoice,height=25,width=50)
EventText.grid(row=3,column=1,columnspan=5)
EventScrollBar.config(command=EventText.yview)
EventText.config(yscrollcommand=EventScrollBar.set)
Quote=("""
...
wd""")
EventText.insert(tk.END,Quote)
EventText.config(state=tk.DISABLED)
I give you two ways of making a Scrollbar.
1) Using tk.Scrollbar
import tkinter as tk
root = tk.Tk()
EventText=tk.Text(root, height=10, width=50)
EventScrollBar= tk.Scrollbar(root, command=EventText.yview, orient="vertical")
EventScrollBar.grid(row=0, column=1, sticky="ns")
EventText.grid(row=0,column=0)
EventText.configure(yscrollcommand=EventScrollBar.set)
Quote=("""Suck\ne\ne\ne\ne\ne\ne\ne\ne\ne\nee\ne\ne\ne\ne\ne\ne\ne\nee\ned\ne\ne\nde\nd\ne\nded\nc\nc\nx\nc\nx\nc\nzc\ns\nds\nx\nwd\ns\nd\nwd""")
EventText.insert(tk.END,Quote)
root.mainloop()
2) Using ScrolledText
import tkinter as tk
from tkinter import scrolledtext
root = tk.Tk()
Quote=("""Suck\ne\ne\ne\ne\ne\ne\ne\ne\ne\nee\ne\ne\ne\ne\ne\ne\ne\nee\ned\ne\ne\nde\nd\ne\nded\nc\nc\nx\nc\nx\nc\nzc\ns\nds\nx\nwd\ns\nd\nwd""")
EventText = scrolledtext.ScrolledText(root, height=10, width=50)
EventText.insert("end", Quote)
EventText.grid(row=0, column=0)
root.mainloop()
Your code shows no attempt to grid the scrollbar.
See below example:
import tkinter as tk
root = tk.Tk()
ybar= tk.Scrollbar(root)
event_text=tk.Text(root, height=10, width=10)
ybar.config(command=event_text.yview)
event_text.config(yscrollcommand=ybar.set)
event_text.grid(row=0, column=0)
ybar.grid(row=0, column=1, sticky="ns")
for i in range(100):
event_text.insert("end", "{}\n".format(i))
root.mainloop()
Just in case you are using grid() in your original code and forgot it here in your example your problem is likely due to the columnspan=5.
If you do that to your text widget then it will sit on top of your scrollbar.
Try something like this when using columnspan:
import tkinter as tk
root = tk.Tk()
ybar= tk.Scrollbar(root)
event_text=tk.Text(root, height=10, width=10)
ybar.config(command=event_text.yview)
event_text.config(yscrollcommand=ybar.set)
event_text.grid(row=0, column=0, columnspan=5)
ybar.grid(row=0, column=5, sticky="ns")
for i in range(100):
event_text.insert("end", "{}\n".format(i))
root.mainloop()
I have written a code in python 2.7 and i have created a root window with two buttons: submit and cancel when i press submit button, a new window opens and previous parent/root window gets iconify. Now i want that when i cut the child window, parent window should be deiconify, i am in a trouble that where to put the condition so that parent window gets deiconify?
from Tkinter import *
root = Tk()
root.title("test window")
root.geometry('400x220+500+250')
root.configure(background="dark gray")
def submit(*args):
root.iconify()
popup_root_window = Toplevel()
popup_root_window.geometry('300x50+550+300')
popup_root_window.resizable(width=False, height=False)
popup_root_window.focus_force()
popup_root_window.grab_set()
popup_root_window_label = Label(popup_root_window, text="new window open successfully.")
popup_root_window_label.pack(anchor=CENTER, padx=10, pady=20)
frame = Frame(root, bd=4, relief="raise", height=100, width=250)
frame.pack(fill="both", padx=70, pady=35)
frame.pack_propagate(0)
submit_button = Button(frame, text="Submit", command=submit, width=10)
submit_button.grid(row=0, column=0)
cancel_button = Button(frame, text="Cancel", width=10)
cancel_button.grid(row=0, column=1)
root.mainloop()
I wrote the below in 3.6 but it should still work in 2.7.
from tkinter import *
class App:
def __init__(self, root):
self.root = root
self.label1 = Label(self.root, text="I'm your main window.")
self.button = Button(self.root, text="Submit", command=self.command)
self.label1.pack()
self.button.pack()
def command(self):
self.root.iconify()
self.top = Toplevel(self.root)
self.label2 = Label(self.top, text="I'm your toplevel window.")
self.label2.pack()
self.top.protocol("WM_DELETE_WINDOW", self.close)
def close(self):
self.top.destroy()
self.root.deiconify()
root = Tk()
App(root)
root.mainloop()
You can use .protocol() on the event WM_DELETE_WINDOW to create a callback whenever the X is selected from the title bar of the designated window.
This is tested and working with Python 3.6 on Windows 10.
You can try this one. i have tested this in python 2.7.13 and it working correctly.
from Tkinter import *
root = Tk()
root.title("test window")
root.geometry('400x220+500+250')
root.configure(background="dark gray")
def submit(*args):
def on_closing(*args):
popup_root_window.destroy()
root.deiconify()
root.iconify()
popup_root_window = Toplevel()
popup_root_window.geometry('300x50+550+300')
popup_root_window.resizable(width=False, height=False)
popup_root_window.focus_force()
popup_root_window.grab_set()
popup_root_window_label = Label(popup_root_window, text="new window open successfully.")
popup_root_window_label.pack(anchor=CENTER, padx=10, pady=20)
popup_root_window.protocol("WM_DELETE_WINDOW", on_closing)
frame = Frame(root, bd=4, relief="raise", height=100, width=250)
frame.pack(fill="both", padx=70, pady=35)
frame.pack_propagate(0)
submit_button = Button(frame, text="Submit", command=submit, width=10)
submit_button.grid(row=0, column=0)
cancel_button = Button(frame, text="Cancel", width=10)
cancel_button.grid(row=0, column=1)
root.mainloop()