Tkinter的!模拟弹出窗口

时间:2017-02-17 13:50:45

标签: python tkinter

我需要帮助。我刚刚开始学习Tkinter,我有一些困难:

图片:带新窗口的主窗口

Image: Main window with a new window

基本上,我想要的是在最初弹出的主窗口(root或master)中创建一个框架。该框架将包含标签和按钮;此外,它将高于其他标签和按钮。与我发布的图片类似。我试图通过创建一个新窗口来实现这一点,但新窗口带有标题,最小化,最大化和关闭按钮,这是我不想要的。我想实现像我发布的图像一样的类似结果。提前谢谢。

1 个答案:

答案 0 :(得分:1)

如果您不需要浮动窗口,则只需创建一个框架并使用place将其放置在窗口的中心。

这是一个基本的例子:

import tkinter as tk

class Popout(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent, background="black", padx=10, pady=10)
        title = tk.Label(self, text="How to play", font=("Helvetica", 16), anchor="w",
                         background="black", foreground="white")
        instructions = tk.Label(self, text="The goal of Klondike is to blah blah blah...",
                                background="black", foreground="white", anchor="w")
        cb = tk.Checkbutton(self, text="Do not show again", highlightthickness=0,
                            background="black", foreground="white")
        oneof = tk.Label(self, text="1 of 6", background="black", foreground="white")
        close_btn = tk.Button(self, text="Close", background="black", foreground="white")
        next_btn = tk.Button(self, text="Next", background="black", foreground="white")

        self.grid_columnconfigure(0, weight=1)
        self.grid_rowconfigure(1, weight=1)

        title.grid(row=0, column=0, columnspan=2, sticky="ew")
        oneof.grid(row=0, column=2, sticky="ne")
        instructions.grid(row=1, column=0, columnspan=3, sticky="nsew", pady=10)
        cb.grid(row=2, column=0, sticky="w")
        close_btn.grid(row=3, column=1, sticky="ew", padx=10)
        next_btn.grid(row=3, column=2, sticky="ew")

root = tk.Tk()
root.geometry("600x400")

p = Popout(root)
p.place(relx=.5, rely=.5, anchor="center")

root.mainloop()