Python 在帧tkinter中的按钮上启动功能
我有一个打开框架的代码。在主框架中,我有一个打开框架2(第01页)的按钮。在第01页是一个按钮,我想让它调用“function1”,这是在顶部定义的(它在我打开程序时启动,但我不想这样),在function1中,我想调用第01页中定义的function2Python 在帧tkinter中的按钮上启动功能,python,tkinter,Python,Tkinter,我有一个打开框架的代码。在主框架中,我有一个打开框架2(第01页)的按钮。在第01页是一个按钮,我想让它调用“function1”,这是在顶部定义的(它在我打开程序时启动,但我不想这样),在function1中,我想调用第01页中定义的function2 Code import tkMessageBox import Tkinter as tk from Tkinter import * from functools import partial import ttk LARGE_FONT
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-打开程序-功能1不应启动,功能2也不应启动
2-打开第01页-功能1和2也停止
3-按下启动f,此时功能1应启动,同时校准功能2也将启动
提前感谢首先,您需要删除
command=function1()
中的括号。其次,由于现在的function2
是Page01
的\uuuu init\uuuuu
的本地函数,因此您不能从其他任何地方调用它。我认为在你尝试做类似的事情之前,你应该仔细阅读。另外,function
是对象,可以作为参数传递,这样你就可以创建一个小部件,在特定情况下给他一个可以调用的函数。