记Python Tkinter控件学习1

Label——标签控件,可显示文本

  • 参数
    • win:父窗体

    • text:显示文本的内容

    • bg:背景色

    • fg:字体色

    • font:font是一个元组

    • width:宽

    • height:高

    • wraplength:行宽

    • justify:设置换行后的对齐方式

    • anchor:设置方位 n s w e center 可以组合使用

      label = tkinter.Label(win,
      text='first Label',
      bg='blue', fg='red',
      font=('黑体', 25),
      wid=10,
      height=10,
      wraplength=100,
      justify='left',
      anchor='w'
      )

Button——按钮控件

  • 参数
    • win:父窗体

    • text:按钮文字

    • command:点击按钮执行动作(可以是lambda表达式或函数名)

      def hello():
      print('hello world')
      button = tkinter.Button(win, text="按钮", command=hello)

Entry——输入控件

  • 参数
    • show:密文显示字符

    • e = tkinter.Variable()(e可理解为输入框对象)

      entry = tkinter.Entry(win, textvariable=e)
      e.set("value") #设置值
      print(e.get()) #获取值

Text——用于显示多行文本

  • 参数
    • win:父窗体

    • height:行数

    • width:行宽

    • text.insert()文本框内容插入方法

      text = tkinter.Text(win, wid=30, height=4)
      str = 'I am the bone of my sword.Steel is my body,and fire is my blood.I have created over a thousand blades.Unknown to Death.Nor known to Life'
      text.insert(tkinter.INSERT, str)

滚动条

  • 滚动条设置的关键在于滚动条和控件的关联

    import tkinter
    win = tkinter.Tk()
    text = tkinter.Text(win, wid=30, height=4)
    str = 'I am the bone of my sword.Steel is my body,and fire is my blood.I have created over a thousand blades.Unknown to Death.Nor known to Life'
    text.insert(tkinter.INSERT, str)
    scroll = tkinter.Scrollbar()
    # 设置位置
    scroll.pack(side=tkinter.RIGHT, fill=tkinter.Y)
    text.pack(side=tkinter.LEFT, fill=tkinter.Y)
    # 关联(此处的关联的单方的)
    scroll.config(command=text.yview) #滚动条向文本框关联
    text.config(yscrollcommand=scroll.set) 文本框向滚动条关联
    win.mainloop()

posted @ 2019-01-14 14:44  明日旋律  阅读(255)  评论(0)    收藏  举报