框架上的小部件没有得到更新

时间:2018-03-12 14:56:21

标签: python tkinter

我创建了两个框架。 在第一帧只是交换框架。 在第二帧我试图显示下一个文件。在这个页面中,我创建了一个名为Back的按钮来交换框架。但是我无法在第2帧看到它。

from Tkinter import *
def swap_frame(frame):
    frame.tkraise()

def get_recent_resultfile():
    print "hi"
    list_of_files = glob.glob('C:/Users/vkandhav/Desktop/velopi*') # * means all if need specific format then *.csv
    latest_file = max(list_of_files, key=os.path.getctime)
    return latest_file

root = Tk()
root.geometry("900x650+220+20")
root.title("Testing")
root.configure(borderwidth="1", relief="sunken",cursor="arrow",background="#BCC3B9",highlightcolor="black")
root.resizable(width=False, height=False)

frame2 = Frame(root, width=900, height=650)
frame1 = Frame(root, width=900, height=650)


#item 1 spinbox
Platform = Spinbox(frame1, values=("SX-16F", "SX-12VP", "SX-16VP", "VSRM-A", "NRNT-A", "FX-8", "DX-48V"), width="32")
Platform.place(x=500, y=200, relheight=0.05)

Button1=Button(frame1, text="Next", width =10, height= 2, bg= "#dbd8d7", command=lambda:swap_frame(frame2))
Button1.place(x=580, y=580)


scrollbar = Scrollbar(frame2)
scrollbar.pack(side=RIGHT, fill=Y)
with open("C:/Users/vkandhav/Desktop/Test.txt", "r") as myfile:
    Result=myfile.read()
print Result
text = Text(frame2,  wrap=WORD, yscrollcommand=scrollbar.set)
text.pack()
text.insert(INSERT, Result)
#B1 = Entry(text="Next",  yscrollcommand=scrollbar.set).pack()
scrollbar.config(command=text.yview)
Button1=Button(frame2, text="Back", width =10, height= 2, bg= "#dbd8d7", command=lambda:swap_frame(frame1))
Button1.place(x=580, y=580)


frame2.grid(row=0, column=0)
frame1.grid(row=0, column=0)

root.mainloop()

1 个答案:

答案 0 :(得分:0)

使用partial将args发送给函数。此外,从以下

中删除了不必要的代码
import sys
if 3 == sys.version_info[0]: ## 3.X is default if dual system
    import tkinter as tk     ## Python 3.x
else:
    import Tkinter as tk     ## Python 2.x

def swap_frame(frame):
    frame.tkraise()

root = tk.Tk()
root.geometry("900x650+220+20")
root.title("Testing")
root.configure(borderwidth="1", relief="sunken",cursor="arrow",
               background="#BCC3B9",highlightcolor="black")
root.resizable(width=False, height=False)

frame1 = tk.Frame(root, width=900, height=650)
frame2 = tk.Frame(root, width=900, height=650, bg="lightblue")

##========== frame 1
text = tk.Text(frame1)
text.grid()
text.insert(tk.INSERT, "this is frame1")

Button2=tk.Button(frame1, text="to frame 2", width =10, height= 2,
               bg= "yellow", command=partial(swap_frame, frame2))
Button2.grid(row=1, column=1)
frame1.grid(row=0, column=0)


##========== frame 2
Button1=tk.Button(frame2, text="to frame 1", width =10, height= 2,
               bg= "yellow", command=partial(swap_frame, frame1))
Button1.grid(row=1, column=1)

text = tk.Text(frame2)
text.grid()
text.insert(tk.INSERT, "this is frame2")

frame2.grid(row=0, column=0)

root.mainloop()