tkinter学习(5)messagebox、pack、grid和place方法

1.messagebox信息弹出框

1.1 代码:

import tkinter as tk #导出tk模块
import tkinter.messagebox #导出弹出信息框
#定义窗口、标题、大小和位置
window = tk.Tk()
window.title('my window')
window.geometry('800x400+500+0')

def hit_me():
#tk.messagebox.showinfo(title='Hi', message='显示信息') # return 'ok'
#tk.messagebox.showwarning(title='Hi', message='警告信息') # return 'ok'
#tk.messagebox.showerror(title='Hi', message='错误信息') # return 'ok'

#tk.messagebox.askquestion(title='Hi', message='询问信息') # return 'yes' , 'no'
#tk.messagebox.askyesno(title='Hi', message='yes或no信息') # return True, False
#tk.messagebox.askokcancel(title='Hi', message='确定或取消信息') # return True, False
tk.messagebox.askyesnocancel(title="Hi", message="不确定或取消信息") # return, True, False, None
#t=tk.messagebox.askyesnocancel(title="Hi", message="询问信息,三个按钮") # return, True, False, None
#print(t)

#-----本机没有这个属性
#tk.messagebox.asktrycancel(title='Hi', message='hahahaha') # return True, False

#定义按钮和位置,pack()居中顶上线显示
tk.Button(window, text='hit me', command=hit_me).pack()

window.mainloop()
View Code

1.2 图1

2.tkinter位置放置方法:pack()、grid()和place()
2.1 代码

import tkinter as tk

window = tk.Tk()
window.title('位置放置的学习')
window.geometry('800x400+500+0')

#方法一:pack的学习
#tk.Label(window, text='冬日暖阳').pack(side='top')
#tk.Label(window, text='冬日暖阳').pack(side='bottom')
#tk.Label(window, text='冬日暖阳').pack(side='left')
#tk.Label(window, text='冬日暖阳').pack(side='right')

#方法二:grid(格子)的学习,适合成批部件的放置,比如简易计算器的按钮放置
#for i in range(4):
#for j in range(3):
#row=行,column=列,padx和pady是外部扩展,ipadx和ipady是内部扩展
#tk.Label(window, text='冬日暖阳').grid(row=i, column=j, padx=10, pady=10)

#方法三:place(位置)比较精准,适合单独一个部件的放置
#x和y是坐标,anchor是錨定位置
tk.Label(window, text='冬日暖阳').place(x=20, y=100, anchor='nw')

window.mainloop()
View Code

2.2 图略

原文地址:https://www.cnblogs.com/ysysbky/p/12202289.html