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

前端 未结 2 2031
深忆病人
深忆病人 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)
    
    0 讨论(0)
  • 2021-02-04 21:18

    As you've taken the liberty to post an answer as a question. I'd like to post a comment as an answer and suggest that perhaps you should contribute this to TkDocs (click their About tab and they talk about contributing to the site).

    I think it'd be better if that site were to improved with more examples than to turn this site into a cookbook. I think you can also contribute to the Active State recipes, and they seem to be the carriers of the torch for Tcl/Tk, so Tkinter stuff makes a lot of sense there too.

    0 讨论(0)
提交回复
热议问题