点击tkinter按钮上的两个脚本之间的链接?

时间:2018-05-06 19:40:07

标签: python tkinter

我的目录中有两个文件(first.pysecond.py)。 first.py有一个按钮。所以当点击first.py gui窗口中的按钮时,它应该被定向到second.py gui窗口。 first.py 窗口照片和second.py 窗口照片。所以点击first.py中的注册按钮,它应该转到second.py中的注册页面。

如何在两个脚本之间建立连接或链接?

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 个答案:

答案 0 :(得分:1)

我解决了你的问题,我猜你想在点击按钮时打开另一个文件。要执行此操作,您需要在按钮单击时导入要加载的文件。并在外部脚本中创建另一个tkinter函数。在运行您的代码时,我甚至遇到了tkinter.TclError: image "pyimage3" doesn't exist错误,截至目前我甚至已修复此问题以获取更多信息,请访问this link。这是我做出所有更改的代码。

    """
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()

和另一个

    #!/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()

现在你可以复制它并使用它。我希望它有所帮助。