【问题标题】:simpledialog.SimpleDialog in python with 2 inputsHpython中的simpledialog.SimpleDialog,带有2个输入H
【发布时间】:2018-05-28 20:47:09
【问题描述】:

我想知道是否有可能在 python 中获得一个带有 2 个输入的 simpledialog.SimpleDialog。文档对此并不清楚,我在网上找不到任何有关此的信息。

我想得到的最终结果是这样的: 一个输入框,每个变量有 2 行

|---------------------|
|Input 1 | ' text abc'|
|---------------------|
|Input 2 | ' Text fgh'|
|---------------------|

提前致以诚挚的问候和感谢

【问题讨论】:

    标签: python python-3.x input tkinter


    【解决方案1】:

    我假设你在追求这样的事情:

    official documentation 解释了如何做所有事情,这远远超出了您现在的需要。

    您需要标签小部件、入口小部件、按钮小部件和打包器几何管理器。

    ZetCode 对你来说可能是一个很好的教程,这个答案部分基于他的代码:

    #### Save this as dual_input.py ####
    #! /usr/bin/env python
    # -*- coding: utf-8 -*-
    
    from tkinter import Tk, Text, TOP, BOTH, X, N, LEFT, RIGHT
    from tkinter.ttk import Frame, Label, Entry, Button
    
    # Good habit to put your GUI in a class to make it self-contained
    class SimpleDialog(Frame):
    
        def __init__(self):
            super().__init__()
            # self allow the variable to be used anywhere in the class
            self.output1 = ""
            self.output2 = ""
            self.initUI()
    
        def initUI(self):
    
            self.master.title("Simple Dialog")
            self.pack(fill=BOTH, expand=True)
    
            frame1 = Frame(self)
            frame1.pack(fill=X)
    
            lbl1 = Label(frame1, text="Input 1", width=6)
            lbl1.pack(side=LEFT, padx=5, pady=10)
    
            self.entry1 = Entry(frame1, textvariable=self.output1)
            self.entry1.pack(fill=X, padx=5, expand=True)
    
            frame2 = Frame(self)
            frame2.pack(fill=X)
    
            lbl2 = Label(frame2, text="Input 2", width=6)
            lbl2.pack(side=LEFT, padx=5, pady=10)
    
            self.entry2 = Entry(frame2)
            self.entry2.pack(fill=X, padx=5, expand=True)
    
            frame3 = Frame(self)
            frame3.pack(fill=X)
    
            # Command tells the form what to do when the button is clicked
            btn = Button(frame3, text="Submit", command=self.onSubmit)
            btn.pack(padx=5, pady=10)
    
        def onSubmit(self):
    
            self.output1 = self.entry1.get()
            self.output2 = self.entry2.get()
            self.quit()
    
    def main():
    
        # This part triggers the dialog
        root = Tk()
        root.geometry("250x150+300+300")
        app = SimpleDialog()
        root.mainloop()
        # Here we can act on the form components or
        # better yet, copy the output to a new variable
        user_input = (app.output1, app.output2)
        print(app.output1)
        # Get rid of the error message if the user clicks the
        # close icon instead of the submit button
        # Any component of the dialog will no longer be available
        # past this point
        try:
            root.destroy()
        except:
            pass
        # To use data outside of function
        # Can either be used in __main__
        # or by external script depending on
        # what calls main()
        return user_input
    
    # Allow dialog to run either as a script or called from another program
    if __name__ == '__main__':
        follow_on_variable = main()
        # This shows the outputs captured when called directly as `python dual_input.py`
        print(follow_on_variable)
    #### End of dual_input.py code dialog code file ####
    
    ### Example of using from code file as opposed to ###
    ### calling directly                              ###
    ### Save this as i_do_work_here.py                ###
    #! /usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import dual_input
    
    received_inputs = dual_input.main()
    
    calculated = int(received_inputs[0]) * int(received_inputs[1])
    print(str(calculated))
    ### End of i_do_work_here.py  ###
    

    【讨论】:

    • 你好。首先感谢您的回复!您的代码运行但是我无法将输出 1 和输出 2 作为自变量获得外部。你能教我怎么做吗?
    • 在main()中添加return语句来恢复user_input。
    • 它没有用。可能是因为没有地方可以回去,因为我在回来之前就辞职了。如果我在尝试之前输入 return be return 语句:窗口不会关闭
    • 您是直接运行脚本还是通过导入运行脚本?当我直接运行它时,它对我有用,即它打印输出。如果您通过导入调用它,则需要显示您在做什么,以便我们确定它有什么问题。
    • return 语句不能在 try 语句之前,因为 try 语句中的root.destroy() 是关闭表单所必需的。 return 必须是 main() 中的最后一条语句。
    猜你喜欢
    • 2021-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-19
    • 1970-01-01
    • 2014-12-20
    • 2016-03-29
    • 2020-01-25
    相关资源
    最近更新 更多