I recently made a chatbot with Python and also GUI using Tkinter for my chatbot. But the problem is I have no idea on how to implement my chatbot into the GUI. Like I made the Chatbot and the GUI separately. I would really be grateful if you guys could help me. Here are Chatbot and GUI codes respectively below:
def start():
print('start')
def second_window():
root=Tk()
root.title('welcome to metis')
root.geometry('400x500')
label2=Label(root,text='welcome to metis')
button2=Button(root,text='hi').place(x=0,y=0)
file_menu = Menu(root)
main_menu = Menu(root)
file_menu.add_command(label="New..")
file_menu.add_command(label="Save As..")
file_menu.add_command(label="Exit")
main_menu.add_cascade(label="File", menu=file_menu)
main_menu.add_command(label="Quit",command=root.quit)
root.config(menu=main_menu)
chatWindow = Text(root, bd=1, bg="black", width="50", height="8", font=("Arial", 23), foreground="#00ffff")
chatWindow.place(x=6, y=6, height=385, width=370)
messageWindow = Text(root, bd=0, bg="black", width="30", height="4", font=("Arial", 23), foreground="#00ffff")
messageWindow.place(x=128, y=400, height=88, width=260)
scrollbar = Scrollbar(root, command=chatWindow.yview, cursor="star")
scrollbar.place(x=375, y=5, height=385)
sendphoto=PhotoImage(file='send.png')
Button3= Button(root, text="Send", width="12", height=5,command=send,
bd=0, bg="#0080ff", activebackground="#00bfff", foreground='#ffffff', font=("Arial", 12))
Button3.place(x=6, y=400, height=88)
root.mainloop()
window=Tk()
window.geometry('500x497')
window.title('metis')
window.config(bg='blue')
bg=PhotoImage(file='rob111.png')
myimage=ImageTk.PhotoImage(Image.open('png-format-16265000621410769899metis.png'))
label=Label(window,image=bg)
label.place(x=0,y=0)
icon=PhotoImage(file='rob111.png')
window.iconphoto(True,icon)
button1=Button(window,text='start',command=second_window).place(x=250,y=250)
window.mainloop()
You can use Tkinter module (python)
ex: label = Label(root, text="hi")
label.pack()
copy the tkinter snippet(available in google)
Related
I have made a code in which you type a question and it fetches an answer from Wikipedia once you press the 'Search' button. However decent I may be at python and tkinter, I'm stuck on this. I know how to add a scrollbar, set it, set it's command, set the y/xscrollcommand and pack it but it's not working and I could use some help.
Thanks in advance for any help I get.
#-----Import library-----#
import wikipedia
from tkinter import *
#-----Create window-----#
window=Tk()
iconpic=PhotoImage(file='Cat.png')
window.iconphoto(False, iconpic)
window.title('ChatCat')
window.geometry('400x517')
window.config(bg='#242121')
window.resizable(width=FALSE, height=FALSE)
#-----Define functions-----#
def two_sentence():
global number_o_sentences
number_o_sentences=2
def three_sentence():
global number_o_sentences
number_o_sentences=3
def four_sentence():
global number_o_sentences
number_o_sentences=4
def five_sentence():
global number_o_sentences
number_o_sentences=5
def reply():
Label(chatWindow, text=(messageWindow.get('1.0', 'end-1c')), wraplength='250', justify='right', fg='#502ec9', bg='#242121').pack(side='top', anchor='e', pady=3)
Label(chatWindow, text=(wikipedia.summary(messageWindow.get('1.0', 'end-1c'), sentences=number_o_sentences)), wraplength='350', justify='left', fg='#b02832', bg='#242121').pack(side='top', anchor='w', pady=3)
#-----Create menu-----#
main_menu=Menu(window)
main_menu.add_command(label="Two", command=two_sentence)
main_menu.add_command(label="Three", command=three_sentence)
main_menu.add_command(label="Four", command=four_sentence)
main_menu.add_command(label="Five", command=five_sentence)
main_menu.add_command(label="Quit", command=window.quit)
window.config(menu=main_menu)
#-----Create conversation window-----#
scrollbar = Scrollbar(window, cursor="target")
scrollbar.pack(side=RIGHT, fill='y')
chatWindow = Text(window, bd=1, bg="black", width="50", height="8", font=("Arial", 23), foreground="#00ffff", yscrollcommand=scrollbar.set)
chatWindow.place(x=6,y=6, height=385, width=370)
messageWindow = Text(window, bd=0, bg="black",width="30", height="4", font=("Arial", 23), foreground="#00ffff", highlightcolor='white', borderwidth=2)
messageWindow.place(x=128, y=400, height=88, width=247)
scrollbar.config(command=chatWindow.yview)
button=Button(window, text="Search", width="12", height=5, bd=0, bg="#2d3652", activebackground="#00bfff",foreground='#ffffff',font=("Arial", 12), command=reply)
button.place(x=6, y=400, height=88)
Label(chatWindow, text='Select the number of sentences you want above and start searching', wraplength='350', justify='left', fg='#b02832', bg='#242121').pack(side='top', anchor='w', pady=3)
#-----Run window-----#
window.mainloop()
I have made a python gui using tkinter and i have been running it through the commandline. But when i tried to open it by double clicking, it shortly shows the window but exits again. I tried commenting out all the places where it uses images thinking that it is because it dosent have file-editing permissions when its run by double click and it worked! But i need to use images in the gui, anyone have solutions?
import tkinter as tk
from PIL import Image, ImageTk
import time
root = tk.Tk()
root.title("Krypto Coin")
root.iconbitmap('icon.ico')
root.configure(bg="#112233")
Frame = tk.Frame(root, width=1200, height=800, bg="#112233")
Frame.grid(columnspan=3, rowspan=5)
icon = Image.open('icon_round.png')
icon = ImageTk.PhotoImage(icon)
icon_label = tk.Label(image=icon, borderwidth=0, highlightthickness=0)
icon_label.place(relx=0.5, rely=0.2, anchor="center")
text = tk.Label(root, text="Welcome to Krypto Coin (KTC)", bg="#112233", fg="#ffffff", font=("Arial", 32))
text.place(relx=0.5, rely=0.45, anchor="center")
text = tk.Label(root, text="Enter id of the receiver and amount", bg="#112233", fg="#ffffff", font=("Arial", 24))
text.place(relx=0.5, rely=0.5, anchor="center")
def trasfer_button_pressed():
time.sleep(0.1)
trasfer_button_text.set("Loading...")
trasfer_button.configure(state="disable")
trasfer_button.configure(bg="#112233")
input_id.configure(state="disable")
input_amount.configure(state="disable")
def trasfer_button_enter(e):
if trasfer_button_text.get() == "Transfer":
trasfer_button.configure(bg="#334455")
def trasfer_button_leave(e):
if trasfer_button_text.get() == "Transfer":
trasfer_button.configure(bg="#223344")
trasfer_button_text = tk.StringVar()
trasfer_button = tk.Button(root, textvariable=trasfer_button_text, command=lambda:trasfer_button_pressed(), bg="#223344", fg="#ffffff", width=15, height=1, font=("Arial", 24), borderwidth=0, highlightthickness=0)
trasfer_button_text.set("Transfer")
trasfer_button.place(relx=0.5, rely=0.65, anchor="center")
trasfer_button.bind("<Enter>", trasfer_button_enter)
trasfer_button.bind("<Leave>", trasfer_button_leave)
input_id = tk.Entry(root, width=48, font = "Arial 14", bg="#334455", fg="#ffffff", borderwidth=0, highlightthickness=0)
input_id.insert(0, "ID")
input_id.place(relx=0.45, rely=0.8, anchor="center", height=30)
input_amount = tk.Entry(root, width=20, font = "Arial 14", bg="#334455", fg="#ffffff", borderwidth=0, highlightthickness=0)
input_amount.insert(0, "Amount")
input_amount.place(relx=0.65, rely=0.8, anchor="center", height=30)
root.mainloop()
So I started creating GUI that I'll use for testbench for electrical engines. My question is why is everything displayed in master window like this?:
Instead of the two frames?
The first time I wrote it, it was working perfectly:
[
but then I wanted to rearrange the code using classes and the problem appeared. I'm using Python 3.7.
My code:
import tkinter as tk
from tkinter import *
from tkinter import messagebox
class Lintebench():
def __init__(self, master):
self.master = master
self.master.title("Linte^2 testbench")
self.master.geometry('1000x600')
#FRAMES
frame = Frame(master, bg='#3e646c').place(relwidth=0.2, relheight=0.4, rely=0.6)
frame2 = Frame(master, bg='#3e646c').place(relwidth=0.8, relheight=0.4, rely=0.6, relx=0.2)
#FRAME 1
self.start_button = Button(frame, text="Start", padx=50, pady=50, bg='green', activebackground='grey', command=self.start_engine).pack()
self.stop_button = Button(frame2, text="Stop", padx=50, pady=50, bg='red', activebackground='grey', command=self.stop_engine).pack()
#FRAME 2
self.parameters = Label(frame2, text="PARAMETERS", font=("Arial", 16), fg='white', bg='#3e646c').place(relx=0)
self.set_parameter = Label(frame2, text="SET", font=("Arial", 16), fg='white', bg='#3e646c').place(relx=0.8)
self.values = Label(frame2, text="VALUES", font=("Arial", 16), fg='white', bg='#3e646c').place(relx=0.4)
self.torque = Label(frame2, text="TORQUE", font=("Arial", 12), fg='white', bg='#3e646c').place(relx=0.7, rely=0.2)
self.velocity = Label(frame2, text="VELOCITY", font=("Arial", 12), fg='white', bg='#3e646c').place(relx=0.7, rely=0.6)
#parameters to set
self.set_torque = Scale(frame2, orient=HORIZONTAL, length=200).place(relx=0.7, rely=0.3)
self.set_velocity = Scale(frame2, orient=HORIZONTAL, length=200).place(relx=0.7, rely=0.7)
def start_engine(*args):
messagebox.showinfo('Information','Engine was started')
def stop_engine(*args):
messagebox.showinfo('Information','Engine was stopped')
def main():
root = Tk()
lintebench = Lintebench(root)
root.mainloop()
if __name__ == '__main__':
main()here
It is because you have chained Frame(...) with place(...) and so frame and frame2 will be None. Separate the chained statement into two statements:
frame = Frame(master, bg='#3e646c')
frame.place(relwidth=0.2, relheight=0.4, rely=0.6)
frame2 = Frame(master, bg='#3e646c')
frame2.place(relwidth=0.8, relheight=0.4, rely=0.6, relx=0.2)
Actually you need to separate all the chained statements in your code if you want to reference the widgets in other place.
Also self.stop_button should be child of frame, not frame2:
self.stop_button = Button(frame, text="Stop", padx=50, pady=50, bg='red', activebackground='grey', command=self.stop_engine)
self.stop_button.pack()
I believe is becuase you're asigning their master as root, and you only have one root and when you make roo.mainloop() that makes them focus.
*So far in my code I show a couple of examples and then ask if the user is ready to take the quiz. I would like to make the window that has the examples to disappear once I press the button that says Yes but I was told I cannot use to commands in one button. What would be the best way to delete the window? *
from tkinter import *
root= Tk()
def clickEvent1():
master = Tk()
mainloop()
def clickEvent2():
master = Tk()
Label2=Label(master, text="Here are the scientific names of some Animals", font=("Times", 13) )
Label2.pack()
Label3=Label(master, text="Wolf=Canis lupus", font=("Arial", 11))
Label3.pack()
Label4=Label(master, text="Lion=Panthera leo",font=("Arial", 11))
Label4.pack()
Label5=Label(master, text="Panda=Ailuropoda melanoleuca", font=("Arial", 11))
Label5.pack()
Label6=Label(master, text="Jellyfish=Medusozoa", font=("Arial", 11))
Label6.pack()
Label7=Label(master, text="Marmoset=Callithrix jacchus", font=("Arial", 11))
Label7.pack()
Label8=Label(master, text="Tiger=Panthera tigris", font=("Arial", 11))
Label8.pack()
Label9=Label(master, text="Zebra=Equus quagga", font=("Arial", 11))
Label9.pack()
Label0=Label(master, text="Would you like to take the quiz now?", font=("Arial", 13))
Label0.pack()
button3=Button(master, text="Yes", font=("Arial", 12), fg="White",bg="Turquoise",height=1, width=10, command=clickEvent1 )
button3.pack()
mainloop()
The code above is where the yes button is used and where the command to make a new window is called.
mylabel=Label(root, text="Welcome to the Animal Trivia Game!", font=("Arial", 14))
mylabel.pack()
label2=Label(root, text="Click 'Start' to begin:)", font=("Arial", 14))
label2.pack()
topFrame=Frame(root)
topFrame.pack()
bottomFrame=Frame(root)
bottomFrame.pack(side=BOTTOM)
leftFrame=Frame(root)
leftFrame.pack(side=LEFT)
rightFrame=Frame(root)
rightFrame.pack(side=RIGHT)
button1=Button(topFrame,text="Start", font=("Arial", 16), fg="White",bg="Turquoise",height=1, width=10,command=clickEvent2)
button1.pack()
mainloop()
I am trying to delete the previous window I created but once I use a command to call it, I receive an error. Can anyone care to explain how this works?
from tkinter import *
root= Tk()
I need help here.
def clickEvent():
master = Tk()
Label2=Label(master, text="Here are the scientific names of some Animals", font=("Times", 13) ).grid(row=0)
Label(master, text="Wolf=Canis lupus", font=("Arial", 11)).grid(row=1)
Label(master, text="Lion=Panthera leo",font=("Arial", 11)).grid(row=2)
Label(master, text="Panda=Ailuropoda melanoleuca", font=("Arial", 11)).grid(row=3)
Label(master, text="Jellyfish=Medusozoa", font=("Arial", 11)).grid(row=4)
Label(master, text="Marmoset=Callithrix jacchus", font=("Arial", 11)).grid(row=5)
Label(master, text="Tiger=Panthera tigris", font=("Arial", 11)).grid(row=6)
Label(master, text="Zebra=Equus quagga", font=("Arial", 11)).grid(row=7)
Label(master, text="Would you like to take the quiz now?", font=("Arial", 13)).grid(row=15)
e1 = Entry(master,textvariable=entryText).grid(row=1, column=1)
button2 = Button(root, text="Yes", command=create_window)
button2.pack()
mylabel=Label(root, text="Welcome to the Animal Trivia Game!", font=("Arial", 14))
mylabel.pack()
label2=Label(root, text="Click 'Start' to begin:)", font=("Arial", 14))
label2.pack()
topFrame=Frame(root)
topFrame.pack()
bottomFrame=Frame(root)
bottomFrame.pack(side=BOTTOM)
leftFrame=Frame(root)
leftFrame.pack(side=LEFT)
rightFrame=Frame(root)
rightFrame.pack(side=RIGHT)
button1=Button(topFrame,text="Start", font=("Arial", 16), fg="White",bg="Turquoise",height=1, width=10,command=clickEvent)
button1.pack()
mainloop()
def clickEvent2():
master=Tk()
mainloop()
def clickEvent2():
master = Tk()
topFrame=Frame(root)
topFrame.pack()
bottomFrame=Frame(root)
bottomFrame.pack(side=BOTTOM)
leftFrame=Frame(root)
leftFrame.pack(side=LEFT)
rightFrame=Frame(root)
rightFrame.pack(side=RIGHT)
button2=Button(topFrame,text="Start Now", font=("Arial", 16), fg="White",bg="Turquoise",height=1, width=10,command=clickEvent)
button2.pack()
mainloop()
root.mainloop()
greatly appreciate it if anyone could help.
You should never have more than one Tk and Mainloop, it will cause many problems in your code. You will have to use a Toplevel instead, which is basically also a new window. To delete a Toplevel, use it's .destroy() method. While to hide it, use it's .withdraw() method.