您好我是tkinter和编码的新手。我正在创建一个名为谷歌自行车的项目。我找到了一个instructable,我想通过添加一个开始菜单看起来更好。它使用python,我使用tkinter为它创建某种起始页面。我正在尝试创建一个按钮,我只需按下它即可启动该程序。这是我用来使tkinter看起来很漂亮的代码,有点像开始菜单。
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()
我正在考虑找到一些关闭tkinter窗口的方法,让python运行google自行车。如果有人能帮助你,非常感谢
答案 0 :(得分:0)
只需向OpenServer
添加一行(这似乎是运行真实程序的函数):
def OpenServer():
execfile("C:/users/Broadway Jewelry/Downloads/server/server2/server.py")
app.destroy()
我还建议,在任何给定的类中,您将该类的实例称为标准self
而不是app
。实际上,它给人的印象是,您在程序结束时实例化的Camacho
对象正在传递并由其app
名称引用。但实际情况并非如此:在Camacho
中,app
可以引用任何Camacho
对象,即使您执行test = Camacho()
之类的操作也是如此。在Camacho
内,test
对象将自称为app
。在其他类中,它更具误导性,因为名为Camacho
的{{1}}对象实际上并非app
,而是app
。< / p>
困惑?即使我觉得它很乱 - 这就是为什么使用controller
更好,并避免在多个范围内使用相同的表观变量名。