0、设置tkinter窗体的宽度、高度: width = 680 height = 540 1、获取电脑屏幕的宽、高: window = tk.Tk() window.title('信息查询') # 获取屏幕宽、 screen_width=window.winfo_screenwidth() s
0、设置tkinter窗体的宽度、高度:
width = 680height = 540
1、获取电脑屏幕的宽、高:
window = tk.Tk()window.title('信息查询')
# 获取屏幕宽、
screen_width=window.winfo_screenwidth()
screen_height=window.winfo_screenheight()
2、计算窗体开始显示的坐标:
x = (电脑屏幕的宽-窗体的宽)/2,y=(电脑屏幕的高-窗体的高)/2
windowX= (screen_width-width)/2windowY=(screen_height-height)/2
3、tkinter messagebox使用:
messagebox.showinfo(title="查询提示1", message="查询名字是的%s!"%gname)4、完整代码:
# -*- coding: UTF-8 -*-import tkinter as tk
from tkinter import *
from tkinter import messagebox
if __name__ == "__main__":
window = tk.Tk()
window.title('信息查询')
width = 680
height = 540
# 设置窗体固定最大值和最小值一样,设置窗体大小不可改变
window.maxsize(width,height)
window.minsize(width,height)
# 获取屏幕宽、
screen_width=window.winfo_screenwidth()
screen_height=window.winfo_screenheight()
# 居中获取宽、高
windowX= (screen_width-width)/2
windowY=(screen_height-height)/2
window.geometry("%dx%d+%d+%d"%(width,height,windowX,windowY))
label = tk.Label(window,text="查询姓名",width=40)
# 声明输入框
e = Entry(window)
# 单人查询
def showONE():
global gname
gname = e.get()
print("查询姓名:",gname)
messagebox.showinfo(title="查询提示1", message="查询名字是的%s!"%gname)
#初始化 按钮名称:以及绑定事件
buttonIns = tk.Button(window, text='单人查询', relief=tk.RAISED, command=showONE,bg="pink")
buttonDel = tk.Button(window, text='清空', relief=tk.RAISED, command="",bg="pink")
# x y:按钮横坐标、纵坐标 width、height:按钮宽、高
label.place(x=90,y=20,width=80, height=30)
e.place(x=160,y=20,width=80, height=30)
buttonIns.place(x=260, y=20, width=80, height=30)
buttonDel.place(x=460, y=20, width=80, height=30)
window.mainloop()
5、测试效果:
(1)居中效果
(2)messagebox 测试效果