第二十天学习python

tkinter 详细学习(一)

下面以一个小代码入门

import tkinter as tk

app=tk.Tk()
app.title("标题")

theLabel=tk.Label(app,text='程序内容')
theLabel.pack()#自动调整位置

app.mainloop()#必须要有这个(运行程序)

输出为

这里写图片描述

下面一段代码

import tkinter as tk

class APP:
    def __init__(self,master):
        frame=tk.Frame(master)
        frame.pack()

        self.hi_there=tk.Button(frame,text='你好',bg="black",fg='white',command=self.say_hi)
        self.hi_there.pack()
    def say_hi(self):
        print("你真好")
root=tk.Tk()
app=APP(root)
root.mainloop()

输出为
这里写图片描述
当你点击你好时
输出为
你真好

from  tkinter import *
app1=tk.Tk()
app1.title("标题")

theLabel=tk.Label(app1,text='程序内容')
theLabel.pack()#自动调整位置

app1.mainloop()#必须要有这个(运行程序)


root = Tk()  # 创建窗口对象的背景色
# 创建两个列表
li = ['1', '2', '3', '4', '5', '6']
movie = ['7', '8', '9']
listb = Listbox(root)  # 创建两个列表组件
listb2 = Listbox(root)
for item in li:  # 第一个小部件插入数据
    listb.insert(0, item)

for item in movie:  # 第二个小部件插入数据
    listb2.insert(0, item)

listb.pack()  # 将小部件放置到主窗口中
listb2.pack()
root.mainloop()  # 进入消息循环

运行如下

这里写图片描述

以上3个例子,前两个为tkinter基础,如何形成一个界面,如何在界面显示

import tkinter
top = tk.Tk()
   标题
   位置
   内容
   位置
top.mainloop()

上述代码为一简单界面输出格式,后续会了解更多。

猜你喜欢

转载自blog.csdn.net/qq_40594554/article/details/81776196