从tkinter窗口提取数据。得到一个类似于Matlab的inputdlg的函数

2024-04-18 03:30:36 发布

您现在位置:Python中文网/ 问答频道 /正文

大家好

不久前我学习了一些MATLAB编码技巧,现在我用python来代替。我想创建一个函数类似于inputdlg从MATLAB。 我在python上做了一点编码,以便创建一个函数从tkinter窗口导入数据,例如,用户提供两个输入(温度1和2),数据保存到一个变量。 但是,当我在另一个脚本中调用函数或将函数代码粘贴到另一个脚本中时,我会遇到问题。窗口出现,但不会关闭,python也不会从运行中返回。我误以为我做了一个无限循环,没有得到解决。有人能帮忙吗?你知道吗

致以最诚挚的问候!你知道吗

def input_dlg():

import tkinter as tk
from tkinter import ttk


class GetEntry():

    def __init__(self, master):


        self.master=master
        self.master.title('Input Dialog Box')
        self.entry_contents=None

        ## Set point entries

        # First point
        self.point1 = ttk.Entry(master)
        self.point1.grid(row=0, column=1)
        self.point1.focus_set()

        # Second point
        self.point2 = ttk.Entry(master)
        self.point2.grid(row=1, column=1)
        self.point2.focus_set()


        # labels
        ttk.Label(text='First Point').grid(row=0, column=0)
        ttk.Label(text='Second Point').grid(row=1, column=0)
        ttk.Button(master, text="Done", width=10,command=self.callback).grid(row=5, column=2)



    def callback(self):
        """ get the contents of the Entries and exit the prompt"""
        self.entry_contents=[self.point1.get(),self.point2.get()]
        self.master.quit()

master = tk.Tk()
GetPoints=GetEntry(master)
master.mainloop()

Points=GetPoints.entry_contents

return list(Points)

Tags: 函数textselfmastertkinterdefcontentscolumn
1条回答
网友
1楼 · 发布于 2024-04-18 03:30:36

Tkinter支持模块simpledialog中的自定义对话框,如果您想让您的生活变得简单。你知道吗

simpledialog包含一个类Dialog,它是专门为您在自定义对话框中继承而设计的。查看中的代码简单对话框.py在目录Python/Lib/tkinter中

在开始编写自己的类之前,请检查对话框的行为是否符合您的要求。请尝试以下代码:

from tkinter import *
from tkinter import simpledialog

root = Tk()     # There must be a root
root.withdraw() # but don't show it

text = simpledialog.askstring('Dialog', 'What?') # Python waits here ... 
print(text)

根窗口不应显示,仅显示对话框。当对话框弹出时,Python将等待您关闭它,然后继续执行其余代码。你知道吗

相关问题 更多 >