Solution: Python3 Tkinter Jump from one window to another with back and next buttons

前端 未结 2 2032
深忆病人
深忆病人 2021-02-04 20:30

I\'ve been studying tkinter in python3 and find it very hard to find good documentation and answers online. To help others struggling with the same problems I decided to post a

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-04 21:07

    Thanks for your work- I used it as inspiration for this example that, while extremely light in terms of the content, is a cool way to make an arbitrary number of windows that you can switch between. You could move the location of the next and back buttons, turn them into arrows, whatever you want.

    from tkinter import *
    master=Tk()
    
    class makeframe(object):
        def __init__(self,i):
            self.i=i
            self.frame=Frame(master)
            self.nextbutton=Button(self.frame,text='next',command=self.next)
            self.nextbutton.grid(column=2,row=0)
            self.backbutton=Button(self.frame,text='back',command=self.back)
            self.backbutton.grid(column=0,row=0)
            self.label=Label(self.frame,text='%i'%(self.i+1)).grid(column=1,row=0)
        def next(self):
            self.frame.grid_forget()
            p[self.i+1].frame.grid()
        def back(self):
            self.frame.grid_forget()
            p[self.i-1].frame.grid()
    
    n=7
    p=[0]*n
    for i in range(n):
        p[i]=makeframe(i)
    p[0].frame.grid()
    p[0].backbutton.config(state=DISABLED)
    p[-1].nextbutton.config(state=DISABLED)
    

提交回复
热议问题