Python tkinter在屏幕上使行完全重量

时间:2017-01-01 13:20:47

标签: python tkinter

我正在努力制作全屏幕。正在全屏工作,但我无法做到以下几点:

1 - 第一行(第0行)缩放到屏幕的最大宽度

2 - 在第1行,第一列和最后一列具有固定宽度并保持在屏幕的左右两侧(这是有效的)

3 - 按钮之间的空标签位于中心

4 - 2个按钮在左右中心对齐

这是我的代码,直到现在:

import Tkinter as tk
from Tkinter import *

class MainApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (StartPage, PageOne, PageTwo):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame
            frame.grid(row=0, column=0, sticky="nsew")
            frame.grid_columnconfigure(0, weight=1)

        self.show_frame("StartPage")

    def show_frame(self, page_name):
        frame = self.frames[page_name]
        frame.tkraise()

class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        #line 0
        label0 = tk.Label(self, text = 'full row', height=3, borderwidth=1)
        label0.configure(relief='raised')
        label0.grid(row=0, column=0, columnspan=12)

        # line 1
        label1 = tk.Label(self, text='0', width=10)
        label1.configure(relief='raised', bg='white')
        label1.grid(row=1, column=0, sticky='w')

        buttonhlp = tk.Button(self, text="HELP", command=close_window)
        buttonhlp.grid(row=1, column=1, columnspan=4)

        label1 = tk.Label(self, text='')
        label1.grid(row=1, column=5)

        label1 = tk.Label(self, text='')
        label1.grid(row=1, column=6)

        buttonquit = tk.Button(self, text="Quit", command=close_window)
        buttonquit.grid(row=1, column=7, columnspan=4)

        label1 = tk.Label(self, text='11', width=10)
        label1.configure(relief='raised', bg='white')
        label1.grid(row=1, column=11, sticky='e')


class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is page 1")
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("StartPage"))
        button.pack()


class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is page 2")
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("StartPage"))
        button.pack()




def close_window ():
    app.destroy()


if __name__ == "__main__":
    app = MainApp()

    app.overrideredirect(True)
    app.geometry("{0}x{1}+0+0".format(app.winfo_screenwidth(), app.winfo_screenheight()))
    app.focus_set()  # <-- move focus to this widget


    app.mainloop()

1 个答案:

答案 0 :(得分:0)

不确定是最好的解决方案,但我让它像这样工作

import Tkinter as tk
from Tkinter import *

class MainApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (StartPage, PageOne, PageTwo):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame
            frame.grid(row=0, column=0, columnspan=12,sticky="nsew")
            frame.grid_columnconfigure(0, weight=1)
            frame.grid_columnconfigure(1, weight=1)
            frame.grid_columnconfigure(2, weight=1)
            frame.grid_columnconfigure(3, weight=1)
            frame.grid_columnconfigure(4, weight=1)
            frame.grid_columnconfigure(5, weight=1)
            frame.grid_columnconfigure(6, weight=1)
            frame.grid_columnconfigure(7, weight=1)
            frame.grid_columnconfigure(8, weight=1)
            frame.grid_columnconfigure(9, weight=1)
            frame.grid_columnconfigure(10, weight=1)
            frame.grid_columnconfigure(11, weight=1)



        self.show_frame("StartPage")

    def show_frame(self, page_name):
        frame = self.frames[page_name]
        frame.tkraise()

class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        #line 0
        label0 = tk.Label(self, text = '')
        label0.configure(relief='raised')
        label0.grid(row=0, column=0, columnspan=12, sticky="nsew")

        # line 1
        label1 = tk.Label(self, text='0', width=10)
        label1.configure(relief='raised', bg='white')
        label1.grid(row=1, column=0, sticky='w')

        buttonhlp = tk.Button(self, text="HELP", command=close_window)
        buttonhlp.grid(row=1, column=1, columnspan=4)

        label1 = tk.Label(self, text='xx')
        label1.grid(row=1, column=5)

        label1 = tk.Label(self, text='tt')
        label1.grid(row=1, column=6)

        buttonquit = tk.Button(self, text="Quit", command=close_window)
        buttonquit.grid(row=1, column=7, columnspan=4)

        label1 = tk.Label(self, text='11', width=10)
        label1.configure(relief='raised', bg='white')
        label1.grid(row=1, column=11, sticky='e')





class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is page 1")
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("StartPage"))
        button.pack()


class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is page 2")
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("StartPage"))
        button.pack()




def close_window ():
    app.destroy()


if __name__ == "__main__":
    app = MainApp()

    app.overrideredirect(True)
    app.geometry("{0}x{1}+0+0".format(app.winfo_screenwidth(), app.winfo_screenheight()))
    app.focus_set()  # <-- move focus to this widget


    app.mainloop()