The ultimate choice of the GUI: Tkinter2 --- Label and Button components assembly

## tkinter of two common components: Label and the Button
   the Label: the Label component is used to output a descriptive label in the interface,
   (for example: the user is prompted "You downloaded video contains content from minors limitations, full 18 years old and then click to view "we can use the Label text output functions to achieve simple, and can add a striking picture in the next, trying to act as a warning and deterrent)!
   renderings:
            Here Insert Picture Description

from tkinter import *

root = Tk()

textLabel = Label(root,text="您所下载的影片含有未成年人限制内容,请满十八周岁后再点击观看!")
textLabel.pack()

photo = PhotoImage(file="18.gif")
imgLabel=Label(root,image=photo)
imgLabel.pack()

mainloop()

   (Analysis: The use from tkinter import *, which means that the module tkinter guide in everything in, if mport tkinter as tk, when the code more than one time, lead to a result that is full screen tk.
      The same Mr. root into a top-level window, and then instantiates a Label component, the first parameter to indicate that this component is placed on the root window, text is the content of the text component, and calling a pick () method lets him automatically emissions about, so just to see his show.
      Meanwhile Label can display a picture, imgLabel = Label (root, image = photo) he just needs this value image that is property of the Label component parameter values can be set, set they should be a picture object tkinter, called PhotoImage, just instantiate this PhotoImage can get a picture object matter, such as here: photo = PhotoImage (file = " 18.gif") passed a picture inside path (ie this pictures name) just fine, here is the support of gif, jpg if not, change the suffix under the picture, then the picture of this position and to Source code. This does not appear in the same position, that is, the same folder, and finally need a mainloop into the events follow)
               Here Insert Picture Description
   ※ to change their position by setting the pick textLabel and imgLabel the () side of the argument, so that text components left-justified, right-justified 18 pictures ban
Here Insert Picture Description
   ※ by \ n textLabel can set the text wrap, not a line of text display was so miserable so long
Here Insert Picture Description

   ※ 可以通过设置组件Label的justify参数来让组件的所有行的文字左对齐,且可以通过padx参数设置文字的左边距
Here Insert Picture Description
   (解析:Label组件的justify参数是用于对齐多行文本,他的参数值有LEFT,CENTER,RIGHT,如果不设置这个参数,他的默认值就是CENTER)

   ※可以通过设置Label组件的compound参数来控制 Label 中文本和图像的混合模式
   (有些时候,可能不想要图片和文字分开,例如你想把图片作为背景,文字显示在图片上面,那么只需要简单的设置Label的 compound 选项)

from tkinter import *

root = Tk()

photo = PhotoImage(file="bg.gif")
theLabel = Label(root,
                 text="床前明月光,\n疑是地上霜。\n举头望明月,\n低头思故乡。",
                 justify = LEFT,
                 image = photo,
                 compound=CENTER,
                 font=("行楷",25),
                 fg = "white",
                 )
theLabel.pack()

mainloop()

                                 Here Insert Picture Description
   (解析:首先还是先root = Tk()实例化出一个root顶层窗口,然后先弄出一个图片对象photo = PhotoImage(file=“bg.gif”),这次只需要一个Label就可以了,
   这个theLabel组件的第一个参数还是表示把这个组件放在root窗口中,
   第二个参数text就是组件的文字内容咯,
   第三个参数justify就是对这个字符串进行一个左对齐,
   第四个参数就是设置图片了,
   第五参数就是设置compound混合模式,设置为center,那么就是文字在图片的正上方显示,
   第六个参数就是设置文字的字体,
   第七个参数fg就是设置文字的前景色就是颜色
   另外compound属性在默认情况下,如果有指定位图或图片,则不显示文本,该选项设置为 bottom,left,right 或 top,那么图像显示在文本下,左,右,上,大家可以自己测试,然后compound属性默认值是 NONE)

   button: Botton 就是按钮。
       (Button 的绝大多数选项(参数) 都和 Label 是一样的,不过,button有一个功能就是可以接收用户的信息,简而言之,Botton 组件就是用于让用户自己说:“干”,然后通过按钮上的文字和图标让用户清楚按下此按钮是干什么用的,Botton 组件有一个叫做 command 的选项,用于指定一个函数或者方法,它的作用就是当用户点下这个按钮的时候,tkinter 就会自动调用这个指定的函数或是方法。)

   ※如果想让Button的文本发生改变,只需要设置一个 textvariable 的选项就可以了,他的用法就是会显示 Tkinter 里边的变量(通常是一个 StringVar 变量)的内容,如果变量被修改,Label 的文本会自动更新。

from tkinter import *

def callback():
    var.set("吹吧你,我才不信呢~")
    

root = Tk()

frame1 = Frame(root)
frame2 = Frame(root)

var = StringVar()
var.set("您所下载的影片含有未成年人限制内容,\n请满十八周岁后再点击观看!")
textLabel = Label(frame1,
                  textvariable=var,
                  justify=LEFT)
textLabel.pack(side=LEFT)

photo = PhotoImage(file="18.gif")
imgLabel=Label(frame1,image=photo)
imgLabel.pack(side=RIGHT)

theButton = Button(frame2,text="我已满 18 周岁",command=callback)
theButton.pack()

frame1.pack(padx=20,pady=20)
frame2.pack(padx=20,pady=20)

mainloop()

                             Here Insert Picture Description
                                             Here Insert Picture Description
       (解析:首先说说实现上面的架构,现在要求实现的要比之前的多一个按钮,当点击按钮后,上部分的文字就会发生改变,这样的话就需要分层,分为上面一个部分,下面一个部分,那就可以用frame作为一个框架,上面一个框架下面一个框架,上面的框架放两个组件textLabel和imageLabel,下面的框架放一个按钮,theButton,
       那么首先就是实例化出两个框架frame1和frame2,参数都是root,frame1 = Frame(root),frame2 = Frame(root)即这两个框架都放到root窗口中,然后把textLabel和imageLabel两个组件的第一个参数都改改为frame1,表示这两个组件都放在框架1中,
       接着在下面加一个按钮theButton = Button(frame2,text=“我已满 18 周岁”,command=callback),第一个参数表示放在第2个框架中,第二个参数就是按钮上的文字内容,第三个参数就command,如果没有这个参数,那么这个button也没什么用,按下去也没什么效果,有了的话按下去就会去执行command后边相关连的函数或者方法,这里取名叫callback
       接着再去上面定义个callback的函数,那么这个callback函数执行就是要改变框架1frame1中的textLabel组件的文本内容,这就要用到textvariable这个选项了,在textLabel组件中加上textvariable,然后他的功能就是可以显示一个变量的内容,比如textvariable=var,那么他就可以显示var这个变量的内容。
       So go above and then define a var variable, but requires this variable must be Tkinter variable, then Tkinter string variable is called the stringVar, after instantiation to var, var then calling his set method to set this variable, Well this is one thing
       that is triggered when the callback function, var set method is called the contents of the variable re-assignment just fine, it will put the original content to overwrite the

       Finally, finally Finally do not forget, theButton have to pack it, and then there frame1, frame2 have to put away their position, and finally had textLabel components inside padx frame1 put inside settings like, while the padding and y you can also adjust the second frame can also adjust, because the holding from the border with the program will look more to tidy)


        Really need to write something more, but it does not matter, old dirty little turtle turtle already finishing extended reading two components, the use of all options and functions are listed out:

       Tkinter widgets: the Label
       Tkinter widgets: Button

Published 247 original articles · won praise 116 · views 280 000 +

Guess you like

Origin blog.csdn.net/w15977858408/article/details/104143388