简体   繁体   English

如何从gui中收到的多行输入写入python文件?

[英]How do I write to a python file from a multi-line input received in a gui?

Im writing a program that's meant to help me with work, but getting stuck on setting up a gui that will receive user input and then use that input to write python code into a file named config.py 我正在编写一个旨在帮助我完成工作的程序,但是在设置一个将接收用户输入的gui然后使用该输入将python代码编写到名为config.py的文件中时会遇到困难

config.py is based off of 5 classes that have multiple lines of text each that function as quicknotes that are written to a separate frame in the gui. config.py基于5个类,这些类具有多行文本,每个行都作为写入gui中单独帧的quicknotes。 I need a user-friendly way to edit these classes instead of opening my text editor (atom in this case) and writing my quicknotes into python code. 我需要一种用户友好的方式来编辑这些类,而不是打开我的文本编辑器(本例中为atom)并将我的quicknotes编写成python代码。

I have the gui set up to receive a text panel for which quicknote is being edited, a text panel for the title, and a multi-line text panel for the actual notes. 我已经设置了gui以接收正在编辑quicknote的文本面板,标题的文本面板以及实际笔记的多行文本面板。 However, because I'm doing the user input insertion into config.py manually, anytime more than one line is put into the body, its no longer formatted correctly 但是,因为我正在手动将用户输入插入到config.py中,所以无论何时将多行放入正文中,它都不再正确格式化

My configGUI.py is the file that is grabbing the user input and writing to the config.py file and here is the code for that 我的configGUI.py是抓取用户输入并写入config.py文件的文件,这里是代码

qnBody_label = wx.StaticText(panel, label='Quicknote Text')
self.body_Control = wx.TextCtrl(panel, style=wx.TE_MULTILINE)
widgets.append(qnBody_label)
widgets.append(self.body_Control)

qnTitle_label = wx.StaticText(panel, label='Quicknote Title')
self.title_Control = wx.TextCtrl(panel)
widgets.append(qnTitle_label)
widgets.append(self.title_Control)

qnBody_label = wx.StaticText(panel, label='Quicknote Text')
self.body_Control = wx.TextCtrl(panel, style=wx.TE_MULTILINE)
widgets.append(qnBody_label)
widgets.append(self.body_Control)

quicknoteNumber = "class QN%s():" % self.number_Control.GetValue()
quicknoteTitle = "\ttitle = '%s'" % self.title_Control.GetValue()
quicknoteBody = "\tnote = (\n\t'%s'\n)" % self.body_Control.GetValue()

Here is an example of what im expecting in config.py: 这是我在config.py中期待的一个例子:

class QN1():
    title = "Heres the title"
    note = (
        'First line of quicknotes\n'
        'Heres some more notes\n'
        'And the last notes\n'
    )

what I actually receive is: 我实际收到的是:

class QN1():
    title = "Heres the title"
    note = (
        'First line of quicknotes
Heres some more notes
And the last notes'
    )

which makes sense given what im telling it to do, but im unsure of how to grab input from a multi-line text panel and format it into python correctly 考虑到我要告诉它做什么,这是有道理的,但我不确定如何从多行文本面板获取输入并正确格式化为python

When you are adding the new line, it is behaving exactly as it should, it prints it on a new line, what you need to do is format it yourself by adding the spaces yourself after the \\n. 当您添加新行时,它的行为完全正确,它将它打印在一个新行上,您需要做的是通过在\\ n之后添加空格来自行格式化。 Add \\t as that is a short version for tab 添加\\ t,因为这是tab的简短版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM