无法使用TKinter导航到窗口中的下一帧

时间:2016-03-09 09:10:46

标签: python-2.7 tkinter navigation

我正在尝试导航到下一个框架,在该框架上将显示新按钮或新标签。我的意思是当用户点击四个按钮的任何按钮并且应该出现frame2时,frame1应该消失。但是下面的代码不起作用。

from Tkinter import *
import tkFileDialog
from PIL import ImageTk, Image

root = Tk()
#my_widget = Widget-name (its container window, ** its configuration options)
def callback():
    path = tkFileDialog.askopenfilename()
    print path

def create_widgets_in_first_frame():
    first_window_label1 = Label(root, text="Analysis of Machine Learning Methods using Titanic Disaster Dataset",justify=CENTER,font=("Helvetica", 20,"bold")).place(x=200,y=20)

    first_window_label5 = Label(root, text="Choose your Method:",justify=LEFT,font=("Helvetica", 15,"italic")).place(x=20,y=180)

    first_window_label2 = Label(root, text="Upload your Titanic datasets .csv extension here:",justify=LEFT,font=("Helvetica", 15,"italic")).place(x=20,y=60)

    first_window_label3 = Label(root, text="Training dataset ->",justify=LEFT,font=("Helvetica", 12)).place(x=50,y=100)
    training_data_path = StringVar()
    first_window_entry1 = Entry(root, textvariable=training_data_path).place(x=350,y=100)
    first_window_button1 = Button(root, text="Browse",command=lambda:training_data_path.set(tkFileDialog.askopenfilename())).place(x=550,y=100)

    first_window_label4 = Label(root, text="Testing dataset ->",justify=LEFT,font=("Helvetica", 12)).place(x=50,y=140)
    testing_data_path = StringVar()
    first_window_entry2 = Entry(root, textvariable=testing_data_path).place(x=350,y=140)
    first_window_button2 = Button(root, text="Browse",command=lambda:testing_data_path.set(tkFileDialog.askopenfilename())).place(x=550,y=140)

    first_window_button3 = Button(root, text="Decision Tree",font=("Helvetica", 15))
    first_window_button3.place(x=50,y=220)
    first_window_button3.bind('<Button-1>', call_second_frame_on_top)

    first_window_button4 = Button(root, text="Random Forests",font=("Helvetica", 15))
    first_window_button4.place(x=350,y=220)
    first_window_button4.bind('<Button-1>', call_second_frame_on_top)

    first_window_button5 = Button(root, text="Logistic Regression",font=("Helvetica", 15))
    first_window_button5.place(x=650,y=220)
    first_window_button5.bind('<Button-1>', call_second_frame_on_top)

    first_window_button6 = Button(root, text="Analysis",font=("Helvetica", 15))
    first_window_button6.place(x=1000,y=220)
    first_window_button6.bind('<Button-1>', call_second_frame_on_top)


def create_widgets_in_second_frame():
    second_window_label6 = Label(root, text="Decision Trees",justify=CENTER,font=("Helvetica", 20,"bold")).place(x=200,y=20)

    print "graph is below"

def call_second_frame_on_top(event):
    first_frame.grid_forget()

    second_frame.grid(column=0, row=0, padx=20, pady=5)


window_width = 1300
window_heigth = 700

first_frame=Frame(root, width=window_width, height=window_heigth)
first_frame['borderwidth'] = 2
first_frame['relief'] = 'sunken'
first_frame.grid(column=0, row=0, padx=20, pady=5)

second_frame=Frame(root, width=window_width, height=window_heigth)
second_frame['borderwidth'] = 2
second_frame['relief'] = 'sunken'
second_frame.grid(column=0, row=0, padx=20, pady=5)

create_widgets_in_second_frame()
create_widgets_in_first_frame()


second_frame.grid_forget()


#root.minsize(width=1300, height=700)
#root.configure(background='lavender')
root.mainloop()

1 个答案:

答案 0 :(得分:1)

问题在于你把所有的小部件都放到root这是整个窗口,所以当你forget框架时,你什么都不会忘记,因为框架中什么都没有。

只需将root更改为first_frame

即可

这是一个有效的代码:

from Tkinter import *
import tkFileDialog
from PIL import ImageTk, Image

root = Tk()
#my_widget = Widget-name (its container window, ** its configuration options)
def callback():
    path = tkFileDialog.askopenfilename()
    print path

def create_widgets_in_first_frame():
    first_window_label1 = Label(first_frame, text="Analysis of Machine Learning Methods using Titanic Disaster Dataset",justify=CENTER,font=("Helvetica", 20,"bold")).place(x=200,y=20)

    first_window_label5 = Label(first_frame, text="Choose your Method:",justify=LEFT,font=("Helvetica", 15,"italic")).place(x=20,y=180)

    first_window_label2 = Label(first_frame, text="Upload your Titanic datasets .csv extension here:",justify=LEFT,font=("Helvetica", 15,"italic")).place(x=20,y=60)

    first_window_label3 = Label(first_frame, text="Training dataset ->",justify=LEFT,font=("Helvetica", 12)).place(x=50,y=100)
    training_data_path = StringVar()
    first_window_entry1 = Entry(first_frame, textvariable=training_data_path).place(x=350,y=100)
    first_window_button1 = Button(first_frame, text="Browse",command=lambda:training_data_path.set(tkFileDialog.askopenfilename())).place(x=550,y=100)

    first_window_label4 = Label(first_frame, text="Testing dataset ->",justify=LEFT,font=("Helvetica", 12)).place(x=50,y=140)
    testing_data_path = StringVar()
    first_window_entry2 = Entry(first_frame, textvariable=testing_data_path).place(x=350,y=140)
    first_window_button2 = Button(first_frame, text="Browse",command=lambda:testing_data_path.set(tkFileDialog.askopenfilename())).place(x=550,y=140)

    first_window_button3 = Button(first_frame, text="Decision Tree",font=("Helvetica", 15))
    first_window_button3.place(x=50,y=220)
    first_window_button3.bind('<Button-1>', call_second_frame_on_top)

    first_window_button4 = Button(first_frame, text="Random Forests",font=("Helvetica", 15))
    first_window_button4.place(x=350,y=220)
    first_window_button4.bind('<Button-1>', call_second_frame_on_top)

    first_window_button5 = Button(first_frame, text="Logistic Regression",font=("Helvetica", 15))
    first_window_button5.place(x=650,y=220)
    first_window_button5.bind('<Button-1>', call_second_frame_on_top)

    first_window_button6 = Button(first_frame, text="Analysis",font=("Helvetica", 15))
    first_window_button6.place(x=1000,y=220)
    first_window_button6.bind('<Button-1>', call_second_frame_on_top)


def create_widgets_in_second_frame():
    second_window_label6 = Label(root, text="Decision Trees",justify=CENTER,font=("Helvetica", 20,"bold")).place(x=200,y=20)
    print "graph is below"

def call_second_frame_on_top(event):
    first_frame.grid_forget()
    second_frame.grid(column=0, row=0, padx=20, pady=5)


window_width = 1300
window_heigth = 700

first_frame=Frame(root, width=window_width, height=window_heigth)
first_frame['borderwidth'] = 2
first_frame['relief'] = 'sunken'
first_frame.grid(column=0, row=0, padx=20, pady=5)

second_frame=Frame(root, width=window_width, height=window_heigth)
second_frame['borderwidth'] = 2
second_frame['relief'] = 'sunken'
second_frame.grid(column=0, row=0, padx=20, pady=5)

create_widgets_in_second_frame()
create_widgets_in_first_frame()


second_frame.grid_forget()


#root.minsize(width=1300, height=700)
#root.configure(background='lavender')
root.mainloop()

请注意,点击任何按钮时获得的标签仍在主root中,因此您必须将其放入second_frame或任何您想要的位置。