python前端tkinter

发布于:2024-04-19 ⋅ 阅读:(23) ⋅ 点赞:(0)

 基础窗口展示:

import tkinter as tk

newWin = tk.Tk()
newWin.title("I'm a new window!")
newWin.geometry("400x300+500+300")  #设置窗口的大小以及初始位置

lab = tk.Label(newWin)  #lab = tk.Label()
lab.config(text = "I'm a new label!")   #config可以理解为调整函数
lab.config(fg = "red", bg = "blue") #字体颜色为红色,背景颜色为蓝色
lab.pack()  #将标签组装到窗口上
#bn = tk.Button(newWin, "I'm a new button!")
bn = tk.Button()
bn["text"] = "click"
bn.pack()
entry = tk.Entry(newWin)    #单行输入框
entry.pack()
tk.mainloop()

 计算窗口展示:

from tkinter import *

newWin = Tk()
newWin.title("I'm a computing window!")
newWin.geometry("400x300")
number1 = StringVar()
number2 = StringVar()

lab1 = Label(text = "Num1")
lab1.grid(row = 0, column = 0)
entry1 = Entry(textvariable = number1)
entry1.grid(row = 0, column = 1)

lab2 = Label(text = "Num2")
lab2.grid(row = 1, column = 0)
entry2 = Entry(textvariable = number2)
entry2.grid(row = 1, column = 1)

lab3 = Label(text = "Result")
lab3.grid(row = 2, column = 0)

def computing():
    a = int(number1.get())
    b = int(number2.get())
    lab3.config(text = "Result is " + str(a + b))

bn = Button(text = "Click", command = computing)
#bn["text"] = "click"
bn.grid(row = 2, column = 1)
newWin.mainloop()