首页 > 解决方案 > 使用弹出式 GUI 中的按钮打开主 GUI 的功能

问题描述

我是 Python 新手,试图让Launch TFL App按钮打开另一个名为“Menu GUI”的 GUI,但我不知道如何处理def open_Menu():下面的函数。我想使用下面的弹出 GUI 作为启动器,将用户带到我的主 GUI。下面代码的唯一问题是,launch TFL app当您单击它时,for 按钮不执行任何操作。这是我当前的代码:

from tkinter import *


root = Tk()

root.title('TFL App')

p = Label(root, text = "TFL Journey Planner", height = "18", width = "250", bg = 'brown', fg = 
'white',
      font = ('Helvetica', '20', 'bold', 'italic'))
p.pack()
root.configure(bg = 'brown')
root.geometry('400x700')

photo = PhotoImage(file = 'trainstation.png')
label = Label(root, image = photo)

label.pack()


****#Buttons****

def open_Menu():
    pass
    

Button1 = Button(root, text = "Launch TFL App", command = open_Menu, bg = "black", fg = 'white', padx 
 = 40,
             pady = 10,
             font = ('Calibri Light', '15', 'bold'))
Button1.pack(padx = 25, pady = 0)


Button2 = Button(root, text = "Exit ", command = root.destroy, bg = "black", fg = 'white', padx = 65, 
pady = 8,
             font = ('Calibri Light', '15', 'bold'))
Button2.pack(padx = 25, pady = 10)

root.mainloop()

我该如何实施open_menu()

下面的代码适用于我的主 GUI,它应该通过上面的 PopUp GUI 打开,但 PopUp GUI 上的按钮不起作用。

from tkinter import *

def find():
# get method returns current text
# as a string from text entry box
    From = From_field.get()
    To = To_field.get()
    travel_modes = mode_field.get()

# Calling result() Function
result(From, To, travel_modes)


# Function for inserting the train string
# in the mode_field text entry box
def train():
    mode_field.insert(10, "train")


# Function for clearing the contents

def del_From():
    From_field.delete(0, END)
    distance_field.delete(0, END)
    duration_field.delete(0, END)


def del_To():
    To_field.delete(0, END)
    distance_field.delete(0, END)
    duration_field.delete(0, END)


def del_modes():
    mode_field.delete(0, END)
    distance_field.delete(0, END)
    duration_field.delete(0, END)



def delete_all():
    From_field.delete(0, END)
    To_field.delete(0, END)
    mode_field.delete(0, END)
    distance_field.delete(0, END)
    duration_field.delete(0, END)


# Driver code
if __name__ == "__main__":
# Create a GUI root
root = Tk()

# Set the background colour of GUI root
root.configure(background = 'light blue')

# Set the configuration of GUI root
root.geometry("600x400")

# Created a welcome to distance time calculator label
headlabel = Label(root, text = 'Welcome to your TFL Journey Planner',
                  fg = 'white', bg = "dark red", height = "0", width = "30",
                  font = ('calibri light', '19', 'italic'))

# Created a From: label
label1 = Label(root, text = "From:",
               fg = 'white', bg = 'black')

# Created a To: label
label2 = Label(root, text = "To:",
               fg = 'white', bg = 'black')

# Created a Distance: label
label4 = Label(root, text = "Distance:",
               fg = 'white', bg = 'black')

# Created a Duration: label
label5 = Label(root, text = "Duration:",
               fg = 'white', bg = 'black')

label6 = Label(root, text = "Choose travelling mode Below: ",
               fg = 'white', bg = 'black')

headlabel.grid(row = 0, column = 1)
label1.grid(row = 1, column = 0, sticky = "E")
label2.grid(row = 2, column = 0, sticky = "E")
label4.grid(row = 7, column = 0, sticky = "E")
label5.grid(row = 8, column = 0, sticky = "E")
label6.grid(row = 3, column = 1)

# Created a text entry box
# for filling or typing the data.
From_field = Entry(root)
To_field = Entry(root)
mode_field = Entry(root)
distance_field = Entry(root)
duration_field = Entry(root)

From_field.grid(row = 1, column = 1, ipadx = "100")
To_field.grid(row = 2, column = 1, ipadx = "100")
mode_field.grid(row = 5, column = 1, ipadx = "50")
distance_field.grid(row = 7, column = 1, ipadx = "100")
duration_field.grid(row = 8, column = 1, ipadx = "100")

# CLEAR Button and attached
# to del_source function
button1 = Button(root, text = "Clear", bg = "light grey",
                 fg = "black", command = del_From)

# Create a CLEAR Button and attached to del_destination
button2 = Button(root, text = "Clear", bg = "light grey",
                 fg = "black", command = del_To)

# Create a RESULT Button and attached to find function
button3 = Button(root, text = "Result",
                 bg = "black", fg = "white",
                 command = find)

# Create a CLEAR ALL Button and attached to delete_all function
button4 = Button(root, text = "Clear All",
                 bg = "light grey", fg = "black",
                 command = delete_all)

# Create a Train Button and attached to train function
button5 = Button(root, text = "Train",
                 bg = "light grey", fg = "black",
                 command = train)

# Create a CLEAR Button and attached to del_modes function
button6 = Button(root, text = "Clear",
                 fg = "black", bg = "light grey",
                 command = del_modes)

button1.grid(row = 1, column = 2)
button2.grid(row = 2, column = 2)
button3.grid(row = 6, column = 1)
button4.grid(row = 9, column = 1)
button5.grid(row = 4, column = 1)
button6.grid(row = 5, column = 2)

root.mainloop()

标签: pythonuser-interfacebuttontkinter

解决方案


这是一个使用from subprocess import call. 您所要做的就是将“YOUR_FILE_NAME”替换为...您的文件名:D

from tkinter import *
from subprocess import call

root=Tk()
root.geometry('200x100')
frame = Frame(root)
frame.pack(pady=20,padx=20)

def open_Menu():
    call(["python", "YOUR-FILE-NAME.py"])

btn=Button(frame,text='Open File',command=Open)
btn.pack()

root.mainloop()

它会是什么样子:

在此处输入图像描述

我希望这对你有用:D


推荐阅读