Tkinter-如何创建标签并向其中移动内容?

时间:2019-09-24 10:57:02

标签: python tkinter

创建了一个应用程序。我想通过添加多个标签来扩展它。

为它们提供名称,例如“主屏幕”,“手册”。我可以在各选项卡之间切换,但是我不确定如何使用Notebook来使用,应用和构建代码。

代码如下:

from tkinter import *

class App():
    def __init__(self,master):
        #Frames
        left_frame = Frame(master)
        right_frame = Frame(master)
        left_frame.pack(side="left", fill="both", expand=True)
        right_frame.pack(side="right", fill="both", expand=True)

        var1 = IntVar()
        var1a = IntVar()

        #Displaying checkboxes and assigning to variables
        self.Checkbox = Checkbutton(right_frame, text="Ingredients present in full (any allergens in bold with allergen warning if necessary)", variable=var1)
        self.Checkbox.grid(column = 1, row = 1, sticky = W)
        self.Checkbox2 = Checkbutton(right_frame, variable = var1a)
        self.Checkbox2.grid(column = 0, row = 1, sticky = W)

       ###FRAME 2###
        #widgets
        self.msg1 = Label(left_frame, text = "Choose here")
        self.msg1.grid(column = 0, row = 0)

root = Tk()
root.minsize(890, 400)
root.title("test only")
app = App(root)
root.mainloop()

如何为以上内容创建标签,并将其命名为Main-Screen,并将以下代码添加到新标签中,并将其命名为Manual

    #widgets
    self.msg1 = Label(left_frame, text = "Click here!")
    self.msg1.grid(column = 0, row = 0)

主要问题:

如何将第一段代码放入一个标签,而另一段代码放入另一个标签?并能够在两者之间切换?

3 个答案:

答案 0 :(得分:0)

您可以使用ttk.Frame:

from tkinter import *
from tkinter import ttk
import os
import glob
from PIL import Image, ImageTk, ImageGrab
from pathlib import Path

class App():
    def __init__(self,master):
        notebook = ttk.Notebook(master)
        notebook.pack()

        #Frames
        left_frame = ttk.Frame(notebook)
        right_frame = ttk.Frame(notebook)
        notebook.add(left_frame, text='Main-Screen')
        notebook.add(right_frame, text='Manual')

        var1 = IntVar()
        var1a = IntVar()

        #Displaying checkboxes and assigning to variables
        self.Checkbox = Checkbutton(right_frame, text="Ingredients present in full (any allergens in bold with allergen warning if necessary)", variable=var1)
        self.Checkbox.grid(column = 1, row = 1, sticky = W)
        self.Checkbox2 = Checkbutton(right_frame, variable = var1a)
        self.Checkbox2.grid(column = 0, row = 1, sticky = W)

       ###FRAME 2###
        #widgets
        self.msg1 = Label(left_frame, text = "Choose here")
        self.msg1.grid(column = 0, row = 0)

root = Tk()
root.minsize(890, 400)
root.title("test only")
app = App(root)
root.mainloop()

答案 1 :(得分:0)

Notebook为您提供了一个选项卡式屏幕。一般示例如下:

from tkinter import *
from tkinter import ttk

root = Tk()
note = ttk.Notebook(root)

ms = ttk.Frame(note)
note.add(ms, text = "Main-Screen")
mn = ttk.Frame(note)
note.add(mn, text = "Manual")
note.pack()

root.mainloop()

根据需要将小部件添加到框架msmn

答案 2 :(得分:0)

如何将notebook添加到您的主窗口,您可以从此link了解更多信息。注释了我的代码以更好地理解它。

from tkinter import *
import tkinter.ttk as ttk

class App():
    def __init__(self,master):
        #Frames
        # Make the notebook
        nb = ttk.Notebook(root)
        nb.pack()

        # Make 1st tab
        right_frame = Frame(nb,)
        # Add the tab
        nb.add(right_frame, text="First tab")

        # Make 2nd tab
        left_frame = Frame(nb)
        # Add 2nd tab
        nb.add(left_frame, text="Second tab")

        nb.select(left_frame)

        nb.enable_traversal()

        var1 = IntVar()
        var1a = IntVar()

        #Displaying checkboxes and assigning to variables
        self.Checkbox = Checkbutton(right_frame, text="Ingredients present in full (any allergens in bold with allergen warning if necessary)", variable=var1)
        self.Checkbox.grid(column = 1, row = 1, sticky = W)
        self.Checkbox2 = Checkbutton(right_frame, variable = var1a)
        self.Checkbox2.grid(column = 0, row = 1, sticky = W)

       ###FRAME 2###
        #widgets
        self.msg1 = Label(left_frame, text = "Choose here")
        self.msg1.grid(column = 0, row = 0)


        # THIS IS THE THIRD FRAME
        self.bottomframe = Frame(master, bg="red", width=400, height=200)
        self.bottomframe.pack(side=BOTTOM, fill=BOTH)

        self.label3 = Label(self.bottomframe, text="THIS IS THE THIRD FRAME")
        # self.label3.grid(column=0, row=0)
        self.label3.pack()            


root = Tk()
root.minsize(890, 400)
root.title("test only")
app = App(root)
root.mainloop()