简体   繁体   中英

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.

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

Use v.get() to get text from Entry and v.set() to change Entry content.

I hope it will be useful.

You have two python files(eg - modules). 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.

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.

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.

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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