Tkinter Tutorial 2: Control Label and Button

Tkinter Tutorial 2: Control Label and Button

This article blog link: http://blog.csdn.net/jdh99 , Author: jdh, reprint please specify.

 

surroundings:

python version: 3.5

Development environment: pyCharm

 

Source code:

Label:

import tkinter as tk
import threading


def main():
    threading.Thread(target=gui_thread).start()


def gui_thread():
    root = tk.Tk()
    app = App(root)
    root.mainloop()


class App:
    def __init__(self, root):
        frame = tk.Frame(root)
        frame.pack()

        # label
        tk.Label(frame, text='这是label1').grid(row=0, column=0)
        tk.Label(frame, text='这是label2').grid(row=0, column=1)

        # label 显示图片
        self.image1 = tk.PhotoImage(file='1.gif')
        tk.Label(frame, image=self.image1).grid(row=1, column=0)


if __name__ == '__main__':
    main()

operation result:

Note: The Label control can display gif format pictures, but not jpg, bmp and other formats.

 

Button:

import tkinter as tk
import threading


def main():
    threading.Thread(target=gui_thread).start()


def gui_thread():
    root = tk.Tk()
    app = App(root)
    root.mainloop()


class App:
    def __init__(self, root):
        frame = tk.Frame(root)
        frame.pack()

        # Button
        tk.Button(frame, text='点我', command=self.say_hello).grid(row=0, column=0)

    @staticmethod
    def say_hello():
        print('Hello World!')


if __name__ == '__main__':
    main()

operation result:

Guess you like

Origin blog.csdn.net/jdh99/article/details/89874154