Tk库实际上有一个函数来实现这一点,尽管它名义上是“私有的”。你可以如下使用它。import tkinter as tk

root = tk.Tk()

root.wm_geometry("800x600")

dialog = tk.Toplevel(root)

root_name = root.winfo_pathname(root.winfo_id())

dialog_name = dialog.winfo_pathname(dialog.winfo_id())

root.tk.eval('tk::PlaceWindow {0} widget {1}'.format(dialog_name, root_name))

root.mainloop()

这将使对话框居中于指定窗口(在本例中为根窗口)。Ref。

在实际应用中

这是我制作的一个版本,因为在现实生活中,您需要在主窗口/窗体的顶部显示一个输入框,通常处于模式(不能单击其他窗口)状态,然后在用户单击“确定”时将其关闭:try:

# for Python2

import Tkinter as tk

except ImportError:

# for Python3

import tkinter as tk

class App:

def __init__(self):

self.HEIGHT = 700

self.WIDTH = 800

root = tk.Tk()

root.width = self.WIDTH

root.height = self.HEIGHT

self.dialogroot = root

self.strDialogResult = ""

self.canvas = tk.Canvas(root, height=self.HEIGHT, width=self.WIDTH)

self.canvas.pack()

frame = tk.Frame(root, bg='#42c2f4')

frame.place(relx=0.5, rely=0.02, relwidth=0.96, relheight=0.95, anchor='n')

# Here is the button call to the InputBox() function

buttonInputBox = tk.Button(frame, text="Input Box", bg='#cccccc', font=60,

command=lambda: self.InputBox())

buttonInputBox.place(relx=0.05, rely=0.1, relwidth=0.90, relheight=0.8)

root.mainloop()

def InputBox(self):

dialog = tk.Toplevel(self.dialogroot)

dialog.width = 600

dialog.height = 100

frame = tk.Frame(dialog, bg='#42c2f4', bd=5)

frame.place(relwidth=1, relheight=1)

entry = tk.Entry(frame, font=40)

entry.place(relwidth=0.65, rely=0.02, relheight=0.96)

entry.focus_set()

submit = tk.Button(frame, text='OK', font=16, command=lambda: self.DialogResult(entry.get()))

submit.place(relx=0.7, rely=0.02, relheight=0.96, relwidth=0.3)

root_name = self.dialogroot.winfo_pathname(self.dialogroot.winfo_id())

dialog_name = dialog.winfo_pathname(dialog.winfo_id())

# These two lines show a modal dialog

self.dialogroot.tk.eval('tk::PlaceWindow {0} widget {1}'.format(dialog_name, root_name))

self.dialogroot.mainloop()

#This line destroys the modal dialog after the user exits/accepts it

dialog.destroy()

#Print and return the inputbox result

print(self.strDialogResult)

return self.strDialogResult

def DialogResult(self, result):

self.strDialogResult = result

#This line quits from the MODAL STATE but doesn't close or destroy the modal dialog

self.dialogroot.quit()

# Launch ...

if __name__ == '__main__':

app = App()

python输入框_如何使用Python创建输入框?相关推荐

  1. python股市_如何使用python和破折号创建仪表板来主导股市

    python股市 始终关注大局 (Keep Your Eyes on the Big Picture) I've been fascinated with the stock market since ...

  2. python创建角色_如何使用 Python 创建一名可操控的角色玩家

    在 这个系列的第一篇文章 中,我解释了如何使用 Python 创建一个简单的基于文本的骰子游戏.在第二部分中,我向你们展示了如何从头开始构建游戏,即从 创建游戏的环境 开始.但是每个游戏都需要一名玩家 ...

  3. aws python库_如何使用Python,AWS和IEX Cloud创建自动更新股市数据的Excel电子表格

    aws python库 Many Python developers in the financial world are tasked with creating Excel documents f ...

  4. 学习python课程_想学习Python吗? 这是我们的免费4小时互动课程

    学习python课程 Python is a popular, versatile and easy-to-learn language. It's the go-to language for AI ...

  5. 2018年python薪资_最好的Python:2017和2018年至今我最喜欢的文章集

    2018年python薪资 我打算发布此收藏集 (My intention with publishing this collection) Last year I only used Medium ...

  6. python程序_正确配置Python应用程序

    让我们来讨论一下如何配置Python应用程序,特别是那些可能存在于多个环境中的应用程序--开发环境.模拟环境.生产环境等等-- 应用程序中使用的工具和框架并不是特别重要,因为我将在下面概述的方法是基于 ...

  7. python 切片_全面解读Python高级特性切片

    大家好,欢迎来到Crossin的编程教室! 众所周知,我们可以通过索引值(或称下标)来查找序列类型(如字符串.列表.元组-)中的单个元素,那么,如果要获取一个索引区间的元素该怎么办呢? 切片(slic ...

  8. 什么是python函数_什么是python函数

    python函数是指组织好的.可重复使用的.用来实现单一或相关联功能的代码段.python函数包含系统中自带的一些函数.第三方函数.以及用户自定义的函数. 函数是可以实现一些特定功能的小方法或是小程序 ...

  9. 比python好_这就是为什么Python比R更好的原因

    目录 介绍 这就是为什么 摘要 介绍 虽然说Python比R更好对我而言是正确的,但对您来说可能并非如此. 当然,您可能会因为各种原因认为R比Python更有用. 即使您反对我的声明,我仍然希望开始进 ...

  10. 易语言 python库_精易Python支持库 (1.1#1205版)发布啦!

    精易Python支持库 (1.1#1205版) 本支持库提供了 6 种库定义数据类型,提供了 87 种命令. 支持库说明 该支持库为易语言调用并执行Python代码.文件提供了支持. 使用本支持库,可 ...

最新文章

  1. python常用库有哪些餐厅_Python常用库整理
  2. AI应用开发基础傻瓜书系列目录
  3. 无法远程xp服务器,五步快速处理在WinXP下IIS无法远程访问的问题
  4. coolfire的八篇入门文章(.txt)
  5. 谷歌代码规范的中文版
  6. JAVA基础语法:java编程规范和常用数据类型
  7. java setmethod_java.util.zip.ZipEntry.setMethod(int method)方法示例
  8. fme坐标转换器_FME坐标点提取
  9. diy 单片机 自动浇花_基于51单片机的自动浇花装置(很详细)
  10. 递归函数实现二进制转换为十进制
  11. eclipse c语言 自动补全,eclipse里头怎么设C/C++的智能提示
  12. JVM的GC算法详解(二)
  13. STM32F4系列ADC最大转换速率及操作条件(以STM32F407ZGT6为例)
  14. 使用Matlab和Minepy工具箱做变量的互信息MIC (maximal information coefficient ) 分析
  15. 华为手机进入工程模式
  16. JZ高中OJ 1420.佳肴
  17. html 手机 touch,手机的html上,touchstart、touchmove、touchend同时存在时,touchmove和touchend失效...
  18. 掘金量化是什么?有哪些特点?
  19. php截取视频第一帧为图片,把视频变成一帧一帧的图片(ae怎么把视频变成单帧) 视频截图视频画面每帧图片截取...
  20. FPGA+DSP的高速AD采集处理开发详解

热门文章

  1. log4j--分路径存储日志
  2. 华为荣耀6 安装失败
  3. 如何通过JQuery动态触发oninput事件
  4. uni-app H5兼容ios问题+微信扫一扫、微信支付等常用api代码封装
  5. 用3D max导入模型时,清除丢失的材质贴图和路径
  6. Web:学生课程表表格/用户注册表单
  7. 【译】 Google: Still in The Search 搜索巨人Google的伟大转变 (三)
  8. SecureCRT 下载,安装,绝佳配色,实用配置,上传下载配置合集
  9. 大学四年生活总结_爆笑动态图:这就是四年大学生活最大的收获
  10. Spring 框架——spring入门教程