首页 > 解决方案 > 在tkinter中单击按钮上的两个脚本之间的链接?

问题描述

我的目录中有两个文件(first.pysecond.py)。有first.py一个按钮。因此,单击first.pygui 窗口中的按钮时,应将其定向到second.pygui 窗口。 first.py窗口照片和second.py窗口照片。因此,单击 first.py 中的注册按钮时,应该会转到 second.py 中的注册页面。

如何做两个脚本之间的连接或链接?

第一个.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()

第二个.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()

标签: pythontkinter

解决方案


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

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

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


推荐阅读