Tkinter将信息从文本框插入python脚本中的特定位置

2024-09-29 02:25:16 发布

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

我最近完成了一个数据标准化脚本,目前正试图通过使用Tkinter创建一个应用程序,使其更加用户友好。我已经设法通过Tkinter运行了数据标准化脚本,但该脚本需要在不同的数据集之间进行细微的更改

我试图实现的是在脚本中的特定位置插入一段用户定义的文本。我在Tkinter上尝试了文本小部件,但是我只在应用程序中打开了脚本,这是我避免做的事情(最好应用程序用户甚至不需要看到原始代码)

我想做的是有一个Tkinter文本框,旁边有一个“Run”按钮。这样,当用户插入一个特定的名称(例如“2020年1月法律会议与会者”)时,它会自动将这段文本放在这里df['Data Identifier'] = ''

我当前的Tkinter代码如下所示:

    def __init__(self):
        super(Root, self).__init__()
        self.title("Python Tkinter Dialog Widget")
        self.minsize(320, 200)
        self.text_area = Text()
        self.text_area.grid(column = 2, row = 3)
        self.labelFrame = ttk.LabelFrame(self, text = "Open File")
        self.labelFrame.grid(column = 0, row = 1, padx = 20, pady = 20)

        self.button()
        self.button1()
        self.button2()
        self.textbox()
        self.textbox1()
        self.textbox2()


    def button(self):
        self.button = ttk.Button(self.labelFrame, text = "Browse a File",command = self.open_file)
        self.button.grid(column = 1, row = 1)

    def button1(self):
        self.button1 = ttk.Button(self.labelFrame, text = "Cleanse Campaign Codes",command = self.standardize)
        self.button1.grid(column = 1, row = 7)

    def button2(self):
        self.button2 = ttk.Button(self.labelFrame, text = "Cleanse Data",command = self.helloCallBack)
        self.button2.grid(column = 1, row = 8)


    def textbox(self):
        self.textbox = ttk.Entry(self.labelFrame)
        self.textbox.grid(column = 6, row = 1)

    def textbox1(self):
        self.textbox1 = ttk.Entry(self.labelFrame)
        self.textbox1.grid(column = 6, row = 2)

    def textbox2(self):
        self.textbox2 = ttk.Entry(self.labelFrame)
        self.textbox2.grid(column = 6, row = 3)

    def helloCallBack(self):
        os.system('python data_cleansing_final.py')

    def open_file(self):
        open_return = filedialog.askopenfile(initialdir =  "C:/", title="Select file to open", filetypes=(("python files", "*.py"), ("all files", "*.*")))
        for line in open_return:
            self.text_area.insert(END, line)

    def standardize(self):
        open_return = open_return.apply(lambda x: difflib.get_close_matches(x, textbox)[0])

root = Root()
root.mainloop()

我将非常感谢任何帮助或建议


Tags: text用户self脚本tkinterdefcolumnbutton
2条回答

您可以在文本框中添加StringVar

self.inputstring = ttk.StringVar(self.lableFrame, value = 'value')
self.textbox2 = ttk.Entry(self.labelFrame, textvariable = self.inputstring)
self.textbox2.grid(column = 6, row = 3)

要读取变量,应使用:

self.inputstring.get()

您可以输入:

text = Entry(root, width=10) text.grid(column=0, row=0)

旁边有一个按钮:

run = Button(root, text="Run", width=10, command=runClicked) run.grid(column=1, row=0)

然后是一个名为runClicked的方法:

def runClicked():
   userText = text.get()

然后变量userText将保存用户键入的任何内容,您可以根据需要使用它

总而言之,您的代码看起来像:

def runClicked():
   userText = text.get()

text = Entry(root, width=10)
text.grid(column=0, row=0)


run = Button(root, text="Run", width=10, command=runClicked)
run.grid(column=1, row=0)

相关问题 更多 >