使用tkinter网格

时间:2017-01-06 18:33:32

标签: python tkinter

我在tkinter中使用.grid相当新,并且想知道如何让l2变量在l1变量下面。运行代码时,第二个标签太远(甚至必须调整窗口大小)。我希望能够将它放在特定的地方(l1下方),但我不确定如何。

谢谢。

示例:

欢迎

请登录访问更多内容

Gap在那里也有点大。

当前代码:

from tkinter import *
from tkinter.ttk import *
root = Tk()
root.geometry("1300x720")
myImage = PhotoImage(file='ssf.png')
label = Label(image=myImage)
label.grid(row=0)
label.image = myImage
l1 = Label(root, text="Welcome", font="Arial 100 bold",  anchor="e").grid(row=0, column=1)
l2 = Label(root, text="Please log-in to continue.", font="Arial 30 bold", anchor="e").grid(row=10, column=1)

Preview of how it looks

2 个答案:

答案 0 :(得分:2)

好吧,您可以使用Frame小部件将文本放入其中:

from tkinter import *
from tkinter.ttk import *
root = Tk()
root.geometry("1300x720")

myImage = PhotoImage(file='ssf.png')
label = Label(image=myImage)
label.grid(row=0)
label.image = myImage

labelFrame = Frame(root)
labelFrame.grid(row=0,column=1)

l1 = Label(labelFrame, text="Welcome", font="Arial 100 bold",  anchor="e")
l1.grid(row=0, column=0)
l2 = Label(labelFrame, text="Please log-in to continue.", font="Arial 30 bold", anchor="e")
l2.grid(row=1, column=0)

Frame小部件位于第0行第1列,包含' l1'和' l2'。

答案 1 :(得分:1)

  1. 请参阅下面修订后的代码中的评论。
  2. 我建议在纸上绘制网格系统以进行可视化 您的布局并将其与您的程序创建的内容进行比较。 激活小部件的背景颜色,例如框架和 标签将帮助您可视化您的创作。
  3. 推荐阅读这些参考资料。 http://www.tkdocs.com/tutorial/grid.html http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/layout-mgt.html
  4. 快乐的编码。 :)

    #from tkinter import *
    #from tkinter.ttk import *
    ## I suggest you abbreviate the imported modules to help you keep track of which
    ## module methods/functions you are using. See below. To be consistent, we will
    ## use ttk widgets where possible.
    
    # Load tkinter for python 3
    import tkinter as tk
    import tkinter.ttk as ttk
    
    root = tk.Tk()
    root.geometry("1300x720")
    
    # Customise style of ttk widgets
    # I have added this to help you visualise the grid system.
    s=ttk.Style()
    s.configure('frame1.TFrame', background='pink')
    s.configure('l0.TLabel', background='blue')
    s.configure('l1.TLabel', background='green')
    s.configure('l2.TLabel', background='brown')
    
    # Create a frame inside root to contain all the widgets.
    # The frame contains a 2x2 grid.
    frame1 = ttk.Frame(root, style='frame1.TFrame', borderwidth=20, relief=tk.SUNKEN )
    frame1.grid(row=0, column=0, rowspan=2, columnspan=2, sticky='nsew')
    
    # Load Image
    # Added tk in fromt of PhotoImage.
    myImage = tk.PhotoImage(file='ssf.png')
    
    # Create a ttk.label to contain image
    ## I added ttk in front of Label. If not, it will mean you will use tk.Label instead of ttk.Label.
    ## Also I added frame1 as the 1st option to the ttk.Label to mean the ttk.Label
    ## is inside frame1.
    ## The "in_=frame1" option is added to grid to mean l0 is grid inside frame1 grid system.
    l0 = ttk.Label(frame1, image=myImage, width=500)  
    l0.grid(in_=frame1, row=0, column=0, sticky='nsew')
    #label.image = myImage
    
    # Create a ttk.Label to contain l1
    l1 = ttk.Label(frame1, text="Welcome", style='l1.TLabel', font="Arial 100 bold", anchor=tk.E)
    l1.grid(in_=frame1, row=0, column=1)
    #l1 = Label(root, text="Welcome", font="Arial 100 bold",  anchor="e").grid(row=0, column=1)
    
    # Create a ttk.Label to contain l2
    l2 = ttk.Label(frame1, text="Please log-in to continue.", style='l2.TLabel', font="Arial 30 bold")
    l2.grid(in_=frame1, row=1, column=1)
    #l2 = Label(root, text="Please log-in to continue.", font="Arial 30 bold", anchor="e").grid(row=10, column=1)
    
    # These configuration settings will let the grid columns and rows scale according
    # to the changing size of the Window i.e. root.
    root.rowconfigure(0, weight=1)
    root.columnconfigure(0, weight=1)
    frame1.rowconfigure(0, weight=1)
    frame1.columnconfigure(1, weight=1)