-3

I have two files in my directory (first.py and second.py). The first.py has a button. So on clicking the button in first.py gui window, it should be directed to second.py gui window. first.py window photo and second.py window photo. So on clicking the sign up button in the first.py, it should go to the sign up page in second.py.

How to do the connection or linking between the two scripts?

first.py

import tkinter as tk

root=tk.Tk()
root.title("My Bank")
root.geometry("500x500")

photo=tk.PhotoImage(file="image1.gif")
label = tk.Label(root, image=photo)
label.image = photo
label.pack()
label.place(x=0, y=0, relwidth=1, relheight=1)

tfm = tk.Frame(root, width=2000, height=50)
tfm.pack(side=tk.TOP)

w = tk.Label(tfm, text="MY bank", font=("Times", "24", "bold"), bg="yellow", anchor="e", fg="black", padx=350, pady=10)
w.pack(fill="both")

bfm = tk.Frame(root, width=2000, height=50, bg="gray")
bfm.pack(side=tk.BOTTOM)

w = tk.Label(root, text="Main Menu", font=("Times", "24", "bold"), bg="black", fg="white", padx=350, pady=10)
w.pack(padx=10, pady=30)

button1 = tk.Button(root, text="Sign Up", width=12, height=1, bg="black",fg="white", bd="8", font=("Helvetica", "12", "bold"))
button1.pack(padx=10, pady=10) 

button2 = tk.Button(root, text="Sign In",  width=12, height=1, 
bg="black",fg="white", bd="8",  font=("Helvetica", "12", "bold"))
button2.pack(padx=10, pady=10)

button3 = tk.Button(root, text="Admin Sign In", width=12, height=1, bg="black",fg="white", bd="8",  font=("Helvetica", "12", "bold"))
button3.pack(padx=10, pady=10)

button4 = tk.Button(root, text="Quit!",  width=5, height=1, bg="black",fg="white", bd="10",  font=("Helvetica", "12", "bold"))
button4.pack(padx=10, pady=10)

root.mainloop()

second.py

import tkinter as tk

root=tk.Tk()
root.title("My Bank")
root.geometry("500x500")

photo=tk.PhotoImage(file="image1.gif")
label = tk.Label(root, image=photo)
label.image = photo
label.pack()
label.place(x=0, y=0, relwidth=1, relheight=1)

tfm = tk.Frame(root, width=2000, height=50)
tfm.pack(side=tk.TOP)

w = tk.Label(tfm, text="MY bank", font=("Times", "24", "bold"), bg="yellow", anchor="e", fg="black", padx=350, pady=10)
w.pack(fill="both")

bfm = tk.Frame(root, width=2000, height=50, bg="gray")
bfm.pack(side=tk.BOTTOM)

w = tk.Label(root, text="Sign Up", font=("Times", "24", "bold"), bg="black", fg="white", padx=350, pady=10)
w.pack(padx=10, pady=30)

e1 = tk.Entry(root, width=20,   font=("Times", "14", "bold"), bd=3, fg="blue")
e1.insert(0, 'Username')
e1.pack(padx=150, pady=10)

e2 = tk.Entry(root, width=20,   font=("Times", "14", "bold"), bd=3, fg="blue")
e2.insert(0, 'Email')
e2.pack(padx=150, pady=10)

e3 = tk.Entry(root, width=20,   font=("Times", "14", "bold"), bd=3, fg="blue")
e3.insert(0, 'Password')
e3.pack(padx=150, pady=10)

button1 = tk.Button(root, text="Sign Up", width=12, height=1, bg="black",fg="white", bd="8", font=("Helvetica", "12", "bold"))
button1.pack(padx=100, pady=20) 

root.mainloop()

1 Answers1

1

I went through your problem and I guess you want to open another file when the button is clicked. To do this you need to import the file which you want to load on button click. And create another tkinter function in the external script. While running through your code i even came across the error which is tkinter.TclError: image "pyimage3" doesn't exist as of now I even fixed this for more information visit this link. Here is the code which i made all the changes.

    """
Spyder Editor

This is a temporary script file.
"""
import second
import tkinter as tk



root=tk.Toplevel()
root.title("My Bank")
root.geometry("500x500")

photo=tk.PhotoImage(file="image1.gif")
label = tk.Label(root, image=photo)
label.image = photo
label.pack()
label.place(x=0, y=0, relwidth=1, relheight=1)

tfm = tk.Frame(root, width=2000, height=50)
tfm.pack(side=tk.TOP)

w = tk.Label(tfm, text="MY bank", font=("Times", "24", "bold"), bg="yellow", anchor="e", fg="black", padx=350, pady=10)
w.pack(fill="both")

bfm = tk.Frame(root, width=2000, height=50, bg="gray")
bfm.pack(side=tk.BOTTOM)

w = tk.Label(root, text="Main Menu", font=("Times", "24", "bold"), bg="black", fg="white", padx=350, pady=10)
w.pack(padx=10, pady=30)

button1 = tk.Button(root, text="Sign Up", command=lambda : second.signup() , width=12, height=1, bg="black",fg="white", bd="8", font=("Helvetica", "12", "bold"))
button1.pack(padx=10, pady=10) 

button2 = tk.Button(root, text="Sign In",  width=12, height=1, 
bg="black",fg="white", bd="8",  font=("Helvetica", "12", "bold"))
button2.pack(padx=10, pady=10)

button3 = tk.Button(root, text="Admin Sign In", width=12, height=1, bg="black",fg="white", bd="8",  font=("Helvetica", "12", "bold"))
button3.pack(padx=10, pady=10)

button4 = tk.Button(root, text="Quit!",  width=5, height=1, bg="black",fg="white", bd="10",  font=("Helvetica", "12", "bold"))
button4.pack(padx=10, pady=10)

root.mainloop()

and the other one

    #!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon May  7 11:09:28 2018

@author: kedar
"""

import tkinter as tk

def signup():

    root=tk.Toplevel()
    root.title("My Bank")
    root.geometry("500x500")

    photo=tk.PhotoImage(file="image1.gif")
    label = tk.Label(root, image=photo)
    label.image = photo
    label.pack()
    label.place(x=0, y=0, relwidth=1, relheight=1)

    tfm = tk.Frame(root, width=2000, height=50)
    tfm.pack(side=tk.TOP)

    w = tk.Label(tfm, text="MY bank", font=("Times", "24", "bold"), bg="yellow", anchor="e", fg="black", padx=350, pady=10)
    w.pack(fill="both")

    bfm = tk.Frame(root, width=2000, height=50, bg="gray")
    bfm.pack(side=tk.BOTTOM)

    w = tk.Label(root, text="Sign Up", font=("Times", "24", "bold"), bg="black", fg="white", padx=350, pady=10)
    w.pack(padx=10, pady=30)

    e1 = tk.Entry(root, width=20,   font=("Times", "14", "bold"), bd=3, fg="blue")
    e1.insert(0, 'Username')
    e1.pack(padx=150, pady=10)

    e2 = tk.Entry(root, width=20,   font=("Times", "14", "bold"), bd=3, fg="blue")
    e2.insert(0, 'Email')
    e2.pack(padx=150, pady=10)

    e3 = tk.Entry(root, width=20,   font=("Times", "14", "bold"), bd=3, fg="blue")
    e3.insert(0, 'Password')
    e3.pack(padx=150, pady=10)

    button1 = tk.Button(root, text="Sign Up", width=12, height=1, bg="black",fg="white", bd="8", font=("Helvetica", "12", "bold"))
    button1.pack(padx=100, pady=20) 

    root.mainloop()

For now you can just copy this and use it. I hope it helps.

Kedar Kodgire
  • 482
  • 6
  • 22