简体   繁体   中英

Using tkinter as a start menu for another python program

Hello I'm new to tkinter and coding. I am creating a project called a google bike. I found an instructable on it and I wanted it to look nicer by adding a start menu. It uses python and I am using tkinter to create a start page of some sort for it. I am trying to create a button where I would just press it in order to launch the program. This is the code I used to make the tkinter to make it look nice and sort of like a start menu.

import Tkinter as tk
from Tkinter import *
import ttk

def OpenServer():
      execfile("C:/users/Broadway Jewelry/Downloads/server/server2/server.py")

class Camacho(tk.Tk):
    def __init__(app):      
        tk.Tk.__init__(app)
        container = tk.Frame(app, width=800, height=500)
        container.pack(side="top", fill="both", expand = True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        app.frames = {}

        for F in (StartPage, Instructionspage, OptionsPage, DemoPage):
            frame = F(container, app)
            app.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        app.show_frame(StartPage)

    def show_frame(app, cont):
        frame = app.frames[cont]
        frame.tkraise()

class StartPage(Frame):
    def __init__(app, parent, controller):
        Frame.__init__(app, parent, background='black', width=800, height=500)
        earth = tk.PhotoImage(file="C:/Users/Broadway Jewelry/Desktop/Bluestamp/earth.gif")
        BGlabel = tk.Label(app,image=earth)
        BGlabel.image = earth
        BGlabel.place(x=300,y=0,width=400,height=400)

        StartProgram = Button(app, text="Start Google Bike",
                          bd=10, command=OpenServer)
        StartProgram.place(x=100,y=75,width=200,height=44)

        Instructions = Button (app, text="Instructions", bd=10,
                           command=lambda: controller.show_frame(Instructionspage))
        Instructions.place(x=100,y=150,width=200,height=44)


        Options = Button (app, text="Options/Setup", bd=10,
                      command=lambda: controller.show_frame(OptionsPage))
        Options.place(x=100,y=225,width=200,height=44)

        Quit = Button (app, text="Exit Program", command=quit, bd=10)
        Quit.place(x=100,y=300,width=200,height=44)

class Instructionspage(Frame):
    def __init__(app, parent, controller):
        Frame.__init__(app, parent, background='black', width=800, height=500)

        label = tk.Label(app, text="Instructions\n \nPedal=go forward\npress button=reverse\nbutton side to steer",
                     font=(200), background='black', fg='white')
        label.place(x=300,y=0,width=400,height=400)

        StartProgram = Button(app, text="Start Google Bike", bd=10, command=OpenServer)
        StartProgram.place(x=100,y=225,width=200,height=44)

        GoBack = Button (app, text="Go Back", bd=10,
                     command=lambda: controller.show_frame(StartPage))
        GoBack.place(x=100,y=300,width=200,height=44)

class OptionsPage (Frame):
    def __init__(app, parent, controller):
            Frame.__init__(app, parent, background='black', width=800, height=500)

            GoBack = Button (app, text="Go Back", width=50, bd=10,
                         command=lambda: controller.show_frame(StartPage))
            GoBack.place(x=100,y=300,width=200,height=44)

            StartProgram = Button(app, text="Start Google Bike", bd=10, command=OpenServer)
            StartProgram.place(x=100,y=225,width=200,height=44)

            ShowDemo = Button (app, text="Show Demo Screen", bd=10,
                           command=lambda: controller.show_frame(DemoPage))
            ShowDemo.place(x=100,y=150,width=200,height=44)

class DemoPage (Frame):
    def __init__(app, parent, controller):
            Frame.__init__(app, parent, background='black', width=800, height=500)
            earth = tk.PhotoImage(file="C:/Users/Broadway Jewelry/Desktop/Bluestamp/Google-Bike.gif")
            BGlabel = tk.Label(app,image=earth)
            BGlabel.image = earth
            BGlabel.place(x=300,y=0,width=400,height=400)

            GoBack = Button (app, text="Go Back", width=50, bd=10,
                     command=lambda: controller.show_frame(OptionsPage))
            GoBack.place(x=100,y=300,width=200,height=44)


app = Camacho()
app.mainloop()

I am thinking of finding some way to close the tkinter window and just have the python running the google bike. If anyone can help thank you very much

Just add a single line to OpenServer (which seems to be the function that runs the real program):

def OpenServer():
    execfile("C:/users/Broadway Jewelry/Downloads/server/server2/server.py")
    app.destroy()

I would also recommend that, within any given class, you refer to an instance of that class as the standard self rather than app . As it is, it's giving the impression that the Camacho object you instantiate at the end of the program is being passed around and referred to by its app name. However, that is not actually the case: in Camacho , app could refer to any Camacho object, even if you did something like test = Camacho() . Within Camacho , that test object would refer to itself as app . In the other classes, it's even more misleading, as the Camacho object named app is actually not app in there at all, but rather controller .

Confused? Even I find it messy - that's why it's better to use self , and avoid using the same apparent variable name in multiple scopes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM