I have created some Python code which is really complicated but it basically asks for an input and then outputs a huge DNA sequence depending on the input given, along with some graphs and many equations. I would like 开发者_StackOverflowto embed this code into the user interface that I will most likely make with wxPython or Tkinter. I don't understand how to plug my code into the user interface. Please Help! Thanks!
A good place to start would be the wxPython tutorial.
The best advice for you is not to start trying to build the actual GUI. Learning GUI programming is not trivial and you should start slowly and work up to the real thing.
Pick a framework (Tkinter, wxPython, Qt) and start with the simplest tutorial you can find. Then try progressively harder tasks until you have enough experience to do a good job with your real task.
The GUI will be handling both your input and output. So if your code is currently a big, long script that reads from and writes to the console, the first thing you'll want to do is refactor it into a class or set of standalone functions (depending on your code) that take the input as arguments, and return the results. If you write it properly, you can even keep your console application while making the class or functions available for other applications to import.
Pseudocode example:
# stuff.py
class StuffDoer:
def __init__(self, val1, val2, ...):
self.val1 = val1
self.val2 = val2
def calculate_sequence(self):
# do some stuff
return sequence
def create_graph(self, target_folder):
# generate the graph and save it
return path_to_graph
if __name__ == '__main__':
# the console interface
val1 = raw_input('Enter value 1:')
val2 = raw_input('Enter value 2:')
s = StuffDoer(val1, val2)
seq = s.calculate_sequence()
print('Sequence: %s' % seq)
path = s.create_graph('/temp')
print('Wrote graph to %s' % path)
You may already know this, but the code inside if __name__ == '__main__':
will only be executed if you run the module directly. If you instead import stuff
from another module (like your GUI code), you'll just have access to the definition of the stuff.StuffDoer
class.
Here's an example of what happens in the GUI code. Upon some interface event like clicking a button, you will call a function that retrieves the input from some fields on your form, gives them to an instance of StuffDoer
, and calls whatever class functions you need to generate your results. If there were no problems, you update the form with the generated results, and you're done. The details of how to do that depend on your GUI toolkit.
Your GUI doesn't need to know how DNA sequences are calculated, and your DNA sequencing module doesn't need to know where the input comes from or how the output is displayed.
Your question isn't terribly clear, but if the user-input you want is Python code, you want to find a Python code editor widget for your GUI-toolkit.
For example, if you were using PyQT, you could use QScintilla, see here for an example
精彩评论