tkinter使用小例子
在python中做一些小的用户交互输入输出时,场景特别简单的时候,我们可能就不是特别想引入wxpython/pyqt这样的图形框架,
那么这个时候python自带的tkinter就是个不错的选择;
下面是一个使用的小例子:
# -*- coding: cp936 -*- import tkinter as tk def Exas(): global root root.destroy() def center(win): """ centers a tkinter window :param win: the root or Toplevel window to center """ win.update_idletasks() width = win.winfo_width() frm_width = win.winfo_rootx() - win.winfo_x() win_width = width + 2 * frm_width height = win.winfo_height() titlebar_height = win.winfo_rooty() - win.winfo_y() win_height = height + titlebar_height + frm_width x = win.winfo_screenwidth() // 2 - win_width // 2 y = win.winfo_screenheight() // 2 - win_height // 2 win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) win.deiconify() def main(): global root root = tk.Tk() root.title("InstagramBot") # root.geometry('180x100+50+50') portInput = tk.StringVar() lblportInput = tk.Label(root, text = '端口:').grid(row = 0, column = 0, padx = 0, pady = 10) entportInput = tk.Entry(root, textvariable = portInput).grid(row = 0, column = 1) btn = tk.Button(root, text = '确定', command = Exas, fg='black', font='黑体 12').grid(row = 5, column = 1) center(root) root.mainloop() portStr = portInput.get() print('===',portStr) if __name__ == "__main__": main()
参考:
https://stackoverflow.com/questions/50438182/user-input-in-dialog-box
https://blog.csdn.net/wuxiushu/article/details/52515926
https://blog.csdn.net/Eider1998/article/details/104725180
https://stackoverflow.com/questions/3352918/how-to-center-a-window-on-the-screen-in-tkinter
https://stackoverflow.com/questions/9987624/how-to-close-a-tkinter-window-by-pressing-a-button
https://stackoverflow.com/questions/17493855/python-3-3-tkinter-window-is-not-defined
https://www.cnblogs.com/lvxiuquan/archive/2012/07/09/2582437.html
https://www.jianshu.com/p/6bc26b3ba6e5
https://stackoverflow.com/questions/4041238/why-use-def-main
https://stackoverflow.com/questions/419163/what-does-if-name-main-do
https://blog.csdn.net/cc7756789w/article/details/46635383
https://juejin.im/post/5b7ec76ce51d4538b149ffc0
https://blog.csdn.net/weixin_35640856/article/details/73614294
https://cloud.tencent.com/developer/article/1092206
- 点赞
- 收藏
- 关注作者
评论(0)