Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
550 views
in Technique[技术] by (71.8m points)

python - Tkinter: multiple pages handling with grid geometry manager

I am trying to generate a GUI for setting some parameters in my embedded SW. I can generate the GUI for each module like this, using the grid geometry manager:

enter image description here

Now, I would like to fusion all pages in one, and jump from one module configuration to another by clicking on a button.

enter image description here

However, I haven t been able to get the proper layout. The solution for multi page handling is using the pack geometry manager, so I can t directly merge the 2 solutions (Gui layout AND multi page handling). I have to decide which geometry manager to use first. I would prefere grid, but when I port the multi paging solution, the page's specific buttons are not available anymore (empty page). When I try the pack solution, I can 't get a way to stack the page specific buttons, so they are available but all on the same row. I checked other solutions, but couldn t find my solution yet. Any help is welcome.

Here is my code for the grid solution, currently running with Python 2.7:

import Tkinter as tk
from Tkinter import *

class Page(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
    def show(self):
        self.lift()

class Page1(Page):
    def __init__(self, *args, **kwargs):
        Page.__init__(self, *args, **kwargs)
        label = tk.Label(self, text="misalignment")
        label.grid(row=1, columnspan=3)

        ## Buttons definition
        # bool parameter
        input_var9= IntVar()
        label_par9 = tk.Label(self, text= "enabled")
        checkBox_enabled = Checkbutton(self,
                                variable=input_var9,
                                onvalue=1,
                                offvalue=0,
                                )
        label_par9.grid(row=2, column=0, sticky=E)
        checkBox_enabled.grid(row=2, column=1)

        # integer parameter
        label_par1 = tk.Label(self, text= "minSpeedStraightDrivingEstimation")
        input_var = tk.DoubleVar(value=0.)
        spin = tk.Spinbox(self, textvariable=input_var, wrap=True, width=8, from_=0, to=100, increment=1)
        slide = tk.Scale(self, variable=input_var, orient='horizontal', length=400)
        label_par1.grid(row=3, column=0, sticky=E)
        slide.grid(row=3, column=1)
        spin.grid(row=3, column=2)


class Page2(Page):
    def __init__(self, *args, **kwargs):
        Page.__init__(self, *args, **kwargs)
        label = tk.Label(self, text="This is page 2")
        label.grid(row=0, columnspan=3)

class Page3(Page):
    def __init__(self, *args, **kwargs):
        Page.__init__(self, *args, **kwargs)
        label = tk.Label(self, text="This is page 3")
        label.grid(row=0, columnspan=3)

class MainView(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        p1 = Page1(self)
        p2 = Page2(self)
        p3 = Page3(self)

        buttonframe = tk.Frame(self)
        container = tk.Frame(self)
        buttonframe.grid()
        container.grid()

        p1.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
        p2.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
        p3.place(in_=container, x=0, y=0, relwidth=1, relheight=1)

        b1 = tk.Button(buttonframe, text="misalignment", command=p1.lift)
        b2 = tk.Button(buttonframe, text="Page 2", command=p2.lift)
        b3 = tk.Button(buttonframe, text="Page 3", command=p3.lift)

        b1.grid(row=0, column=0)
        b2.grid(row=0, column=1)
        b3.grid(row=0, column=2)

        p1.show()

if __name__ == "__main__":
    root = tk.Tk()
    root.title('Carma parameter client')
    main = MainView(root)

    root.wm_geometry("1000x800")
    root.mainloop()

One possibility is that the container frame, which holds the module specific widgets, is not defined properly. It was working with pack, but defined as:

buttonframe.pack(side="top", fill="x", expand=False)
container.pack(side="top", fill="both", expand=True)

And now, it is like this:

buttonframe.grid()
container.grid()

My current hypothesis, is that the container frame is not expanded, hence nothing can be displayed. I tried to add a sticky= S+W+E in its grid definition, but it doesn t work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...