简体   繁体   English

如何在python中将代码集成到应用程序中

[英]How to integrate code into application in python

This is my application code:这是我的应用程序代码:

#GUI
from tkinter import *
#Create the window
root = Tk()
#Modify root window
root.title("Simple Bot")
root.geometry("500x400")
#Kick off the event loop
root.mainloop()

This is the bot code:这是机器人代码:

botName = input("Bot: Hello User, my name is ______ please name me: ");
botName = botName + ": ";
print(botName + "Thankyou for naming me.");
firstName = input(botName + "What is your name? ");
print(botName + "Hello, " + firstName);
favourite =  input(botName + "Tell us what you like doing the most? ");
print(botName + "Nice, I like to do that as well.");
print(botName + "If you have any Questions just ask.");

How do I get the bot code to work inside of the application I have created, and what other code do I have to add for the bot code to work in the application.我如何让机器人代码在我创建的应用程序中工作,以及我必须添加哪些其他代码才能让机器人代码在应用程序中工作。

PS I am new to python and trying out different things. PS我是python的新手并尝试了不同的东西。

As a simple example to begin, try this:作为一个简单的例子开始,试试这个:

from  Tkinter import *
import tkMessageBox

root = Tk()

root.title("Simple Bot")
root.geometry("500x80")

def msg(ev=None):
   tkMessageBox.showinfo("Message", v.get() + " Thank you for naming me.")


root.bind('<Return>', msg)

L = Label(root, text="Bot: Hello User, my name is ______ please name me: ", font=("Helvetica", 14)) 

v = StringVar()
E = Entry(root, textvariable=v, font=("Helvetica", 16))

L.pack()
E.pack(side=BOTTOM, fill=BOTH, expand=1)

root.mainloop()

print is replaced with tkMessageBox and input with Entry print 替换为tkMessageBox并使用Entry

Use v.get() to get text from Entry and v.set() to change Entry content.使用v.get()Entry获取文本,使用v.get()更改 Entry 内容。

I hope it will be useful.我希望它会很有用。

You have two python files(eg - modules).您有两个 python 文件(例如 - 模块)。 First file - gui, second - some logic part(firstname, favourite things and etc) You should create some text area(for example - label or message box) where you will display information.第一个文件 - gui,第二个 - 一些逻辑部分(名字,喜欢的东西等) 您应该创建一些文本区域(例如 - 标签或消息框),您将在其中显示信息。

You can start your application, after that you start your bot code(import it, i think), where you get information.您可以启动您的应用程序,然后启动您的机器人代码(我认为是导入它),您可以在其中获取信息。 Then you put your information in text area.Or you can firstly start bot code, and after that start application code, where you put firstname and favourite in label or text.然后你把你的信息放在文本区。或者你可以先启动机器人代码,然后启动应用程序代码,你把名字和收藏夹放在标签或文本中。

For example in code below we put bot name in Text Area.例如在下面的代码中,我们将机器人名称放在文本区域中。 bot.py - it's your bot code module. bot.py - 这是你的机器人代码模块。

from tkinter import *
import bot

#Create the window
root = Tk()
#Modify root window
root.title("Simple Bot")
root.geometry("500x400")

text = Text(root)

text.insert(INSERT, bot.firstName)
text.pack()

#Kick off the event loop
root.mainloop()

Assuming these are two different .py files, you can simply use the import statement if all you need are the variables.假设这是两个不同的 .py 文件,如果您只需要变量,您可以简单地使用 import 语句。

import bot_code

If you'd like to use the entire chuck of code at once inside of your application, you could put it in a function like so:如果你想在你的应用程序中一次使用整个代码库,你可以把它放在一个像这样的函数中:

def bot():
    botName = input("Bot: Hello User, my name is ______ please name me: ");
    botName = botName + ": ";
    print(botName + "Thankyou for naming me.");
    firstName = input(botName + "What is your name? ");
    print(botName + "Hello, " + firstName);
    favourite =  input(botName + "Tell us what you like doing the most? ");
    print(botName + "Nice, I like to do that as well.");
    print(botName + "If you have any Questions just ask.");

and then just import as stated above.然后如上所述导入。 Now you can call the function inside of your application and all the code will run.现在您可以在应用程序内部调用该函数,并且所有代码都将运行。

import bot_code
bot() # This makes your botcode run

I am assuming your bot code is in a file 'bot_code.py'我假设你的机器人代码在一个文件“bot_code.py”中

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

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