How to not open window - python-3.x

from tkinter import *
def firstPage():
def goSecond():
root.destroy()
secondPage()
root = Tk()
text = Label(root, text="1")
text.pack()
btn = Button(root, text="go to 2", command=goSecond)
btn.pack()
root.mainloop()
def secondPage():
def goFirst():
root.destroy()
firstPage()
root = Tk()
text = Label(root, text="2")
text.pack()
btn = Button(root, text="go to 1", command=goFirst)
btn.pack()
root.mainloop()
firstPage()
I made this code and I want it to open a new page, but it open a new window, I understand why but I don't know how to open a page instead of open a window.

Related

change the position of already packed widget in tkinter

Here is the code,
from tkinter import *
root = Tk()
update_button = Button(root, text='Update')
update_button.pack()
def button():
frame1 = Frame(root)
frame1.pack()
button1 = Button(frame1, text="Button 1")
button1.pack(side=LEFT)
button2 = Button(frame1, text="Button 2")
button2.pack(side=LEFT)
button()
root.mainloop()
I want button1 to be stacked upon button2 when I click update button.
Help please.

Tkinter text-widget insertion of images

I made a text widget to write questions. I want to insert images when add image button is pressed. How can I do that? When I choose another image the first one is deleted. Right now I am able to add one image using the code:
import tkinter as tk
root = tk.Tk()
root.geometry('800x520+0+0')
global img
img = tk.PhotoImage(file="quiz.gif")
def add_img():
T.image_create(tk.INSERT, image=img)
tk.Button(root, text="Add Image", font=('Verdana',8),
command=add_img).place(x=690, y=0)
T = tk.Text(root, width=65, height=17, padx=10, pady=10, font=('Verdana',
14), wrap='word')
T.place(x=0, y=0)
root.mainloop()
I want to add different images when chosen using tk-listbox.
Thanks for the help.
I used dictionary to solve the problem.
import tkinter as tk, glob
root = tk.Tk()
root.geometry('800x520+0+0')
Images = {}
for infile in glob.glob('*.gif'):
img = infile[:-4]
if img not in Images:
Images[img] = tk.PhotoImage(file=infile)
def add_img():
global listbox
listbox = tk.Listbox(root, font=('Verdana',9), width=12)
listbox.place(x=60,y=2)
for infile in glob.glob('*.gif'):
listbox.insert(tk.END, infile[:-4])
listbox.bind('<<ListboxSelect>>',CurSelet)
def CurSelet(event):
fn = listbox.get(tk.ANCHOR)
listbox.destroy()
T.image_create(tk.INSERT, image=Images[fn])
tk.Button(root, text="Add Image", font=('Verdana',8),
command=add_img).place(x=690,y=0)
T = tk.Text(root, width=65, height=17, padx=10, pady=10, font=('Verdana',
14), wrap='word')
T.place(x=0, y=50)
root.mainloop()

How to open a child frame in a single application window in tkinter?

What I want is when I click on a button a new window should get open and that window should be a child of the same main window, But what I am getting is a new Instance of a new window. How do I solve this in tkinter?
Here is the screenshot of what I do not want, Every time a new instance of window is getting is created, I want to make a child instance of same main window:
def login_success():
def c1():
top = Toplevel()
top.title("c1")
top.geometry("1000x600")
def c2():
top = Toplevel()
top.title("c1")
top.geometry("1000x600")
def c3():
top = Toplevel()
top.title("c2")
top.geometry("1000x600")
def write_frames():
top = Toplevel()
top.title("t2")
top.geometry("1000x600")
b1 = Button(top, text="c1", command=c1)
b1.pack()
b2 = Button(top, text="c2", command=c2)
b2.pack()
b3 = Button(top, text="c3", command=c3)
b3.pack()
def write_instructions():
top = Toplevel()
top.title("t1")
top.geometry("1000x600")
root = Tk()
root.geometry("1000x600")
button1 = Button(root,
text="Frames",
command=write_frames)
button1.pack()
button2 = Button(root,
text="Instructions",
command=write_instructions)
button2.pack()
You just have to pass root as a parameter to every function if i understand correctly what you mean
def login_success():
def c1(root):
top = Toplevel(root)
top.title("c1")
top.geometry("1000x600")
def c2(root):
top = Toplevel(root)
top.title("c1")
top.geometry("1000x600")
def c3(root):
top = Toplevel(root)
top.title("c2")
top.geometry("1000x600")
def write_frames(root):
top = Toplevel(root)
top.title("t2")
top.geometry("1000x600")
b1 = Button(top, text="c1", command=lambda: c1(root))
b1.pack()
b2 = Button(top, text="c2", command=lambda: c2(root))
b2.pack()
b3 = Button(top, text="c3", command=lambda: c3(root))
b3.pack()
def write_instructions(root):
top = Toplevel(root)
top.title("t1")
top.geometry("1000x600")
root = Tk()
root.geometry("1000x600")
button1 = Button(root,
text="Frames",
command=lambda: write_frames(root))
button1.pack()
button2 = Button(root,
text="Instructions",
command=lambda: write_instructions(root))
button2.pack()

How can i create a Modal Dialogue Box by messagebox.showerror

How can I create a Modal Dialogue Box by messagebox.showerror?
messagebox.showerror("Error", "No downloader.exe found")
When I create a messagebox, I found I can move the root windows.
and i need to create a Modal Dialogue Box like filedialog.askopenfilename.
filedialog.askopenfilename(initialdir = self.get_path()+ '/bin', filetypes=[("BIN Files", ".bin")])
here's the codes:
import tkinter
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import os
class Application(Frame):
def createWidgets(self, main_frame):
#self.llabel = Label(main_frame, text="Ready", width=20, bg="turquoise", font = ftLabel)
#self.llabel.grid(row=0, column=0, sticky=W+E) #columnspan=2
self.frame1 = Frame(main_frame)
self.frame1.grid(row=0, column=0, columnspan=2, sticky=W+E+N+S)
self.addr = StringVar()
self.addrtext = Entry(self.frame1, width=20, textvariable = self.addr)
self.addrtext.grid(row=0, column=0, sticky=W+E+N+S)
self.addr.set("0x0")
self.bfile = Button(self.frame1, text='BIN File', width=20)
self.bfile.grid(row=0, column=1, sticky=W+E+N+S)
messagebox.showerror("Error", "No downloader.exe found")
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack(fill=BOTH, expand=1)
main_frame = Frame(master)
main_frame.pack(fill="y", expand=1)
self.createWidgets(main_frame)
self.dl_thread = 0
if __name__=="__main__":
root = Tk()
#lock the root size
root.resizable(False,False)
app = Application(master=root)
app.mainloop()
I tried your code and messagebox.showerror is modal to me.
Maybe there is something else in your code (threads?) or maybe it's
dependent on your environment.
For reference, my entire code:
from tkinter import *
from tkinter import messagebox
root = Tk()
def do(): messagebox.showerror("Error", "No downloader.exe found")
b = Button(root, text='Dialog', command=do)
b.pack()
root.mainloop()
If that doesn't work you might want to take a look at: Tkinter messagebox not behaving like a modal dialog

How can i edit this code i've made so far so that when i click the button 'signup' it closes that gui and opens the next one

i am using tkinter to make a gui and have made various different buttons and now i have made all this i am unsure how to correctly make the first gui box close as the second one opens (sign_in function)
from tkinter import *
class login:
def __init__(self, master):
frame = Frame(master)
frame.grid()
self.button1 = Button(frame, text="signup", fg="green",command=self.sign_in)
self.button2 = Button(frame, text="sign in", fg="black",)
self.button3 = Button(frame, text="quit", fg="red", command=frame.master.destroy)
self.button1.grid(stick=W)
self.button2.grid(stick=W)
self.button3.grid(stick=W)
def sign_in(self):
frame = Frame()
frame.grid()
name = Label(root, text="Name: ")
password = Label(root, text="password: ")
entry1 = Entry(root)
entry2 = Entry(root)
name.grid(row=0, sticky=E)
password.grid(row=1, sticky=E)
entry1.grid(row=0, column=1)
entry2.grid(row=1, column=1)
c = Checkbutton(root, text="keep me logged in")
c.grid(columnspan=2, sticky="w")
root = Tk()
account=login(root)
root.mainloop()
Your code contains some indentation errors so I'll just go by your question.
when i click the button 'signup' it closes that gui and opens the next one
You can do so by first withdrawing your root window like this: root.withdraw() which will hide your original window. Then create a Toplevel window like this: newWindow = tk.Toplevel(root) to create a new window. You will just need to place these lines in the button command call.
Here's what you can change in the sign_in note that I changed all the masters to frame and not root:
def sign_in(self):
root.withdraw()
frame = Toplevel(root)
name = Label(frame, text="Name: ")
password = Label(frame, text="password: ")
entry1 = Entry(frame)
entry2 = Entry(frame)
name.grid(row=0, sticky=E)
password.grid(row=1, sticky=E)
entry1.grid(row=0, column=1)
entry2.grid(row=1, column=1)
c = Checkbutton(frame, text="keep me logged in")
c.grid(columnspan=2, sticky="w")

Resources