在帧tkinter中的按钮上启动功能

时间:2015-04-23 08:51:17

标签: python tkinter

我有这个打开框架的代码。在主框架中,我有一个打开frame2的按钮(Page01)。在page01上是一个按钮,我想让它调用“function1”,它在顶部定义(并且它在我打开程序时启动,但我不想要这个)并且在function1中我想要调用function2,它在Page01中定义。

Code

import tkMessageBox
import Tkinter as tk
from Tkinter import *
from functools import partial
import ttk


LARGE_FONT= ("Verdana", 12)

def function1():
    print "Function 1 started"
    #function2()

class ChangePages(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)
        container.pack()
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        self.frames = {}
        for F in (MainPage, Page01):

            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(MainPage)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()

class MainPage(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self,parent)
        def C(*args): return partial(self.option_changed, *args)

        f = Frame(self)
        f.pack(side='top')

        btnpage01=Button(f,text='Go to Pag 01',fg='blue',font=('Helvetica',18),height=1, width=10,command=lambda: controller.show_frame(Page01))
        btnpage01.grid(row=1,column=1)

class Page01(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)
        f = Frame(self)
        f.pack(side='left')

        def function2():
            print "Function 2 started"

        bttest = Button(f,text='Start f',fg='blue',font=('Helvetica',26),height=1,width=10,command=function1() )
        bttest.grid(row=1,column=1)

#Root loop
app = ChangePages()
app.geometry('300x200+0+0')
app.title('Title ')
app.mainloop()

所以我想要的是: 1-打开程序 - function1不应该启动,也不应该启动function2 2- open page01 - 也停止了function1和2 3-按开始f,现在功能1应该启动,同时将cal function2启动

提前致谢

0 个答案:

没有答案