【问题标题】:Link between two scripts on a button click in tkinter?在 tkinter 中单击按钮上的两个脚本之间的链接?
【发布时间】:2018-05-06 19:40:07
【问题描述】:

我的目录中有两个文件(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()

【问题讨论】:

  • it 应该定向到second.py gui 窗口”到底是什么意思?第一个窗口会发生什么?您是否尝试过编写任何代码,如果是,为什么不在您的问题中?
  • 请在您的问题中加入minimal reproducible example
  • 我贴出了first.py和second.py的窗口图片
  • 我们不需要图片,我们需要看你的代码。
  • 我已经发布了代码。请帮帮我。

标签: python tkinter


【解决方案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()

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-24
    • 1970-01-01
    • 2022-10-15
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 2023-01-23
    • 1970-01-01
    相关资源
    最近更新 更多