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.
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 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)
I'm trying to give myself Excercises using imports and keeping programs and modules separate from each other. In this particular excercise I've given myself, I have a basic feet-to-inches and inches-to-feet converter.
Everything works except for one problem. Where the answer is displayed on the GUI, a new answer covers the old answer, rather than replace.
import tkinter as tk
from tkinter import E, W
import Converter
window = tk.Tk()
window.title("Converter")
answer_label = tk.Label(window, text="No Answer")
answer_label.grid(row=5)
def feet_to_inches_function(old_label):
old_label.grid_forget()
answer_label = tk.Label(window, text=Converter.feet_to_inches(int(entry_bar.get())))
answer_label.grid(row=5)
def inches_to_feet_function(old_label):
old_label.grid_forget()
answer = tk.Label(window, text=Converter.inches_to_feet(int(entry_bar.get())))
answer.grid(row=5)
title_label = tk.Label(window, text="Convert")
entry_bar = tk.Entry(window, font=('HELVETICA', 10))
fti_button = tk.Button(window, text="Feet to Inches", command=lambda: feet_to_inches_function(answer_label))
itf_button = tk.Button(window, text="Inches to Feet", command=lambda: inches_to_feet_function(answer_label))
quit_button = tk.Button(window, text="Quit", command=window.destroy)
title_label.grid(row=0, column=0, sticky=W)
entry_bar.grid(row=1, columnspan=2, sticky=(E, W))
fti_button.grid(row=3, column=0)
itf_button.grid(row=3, column=1)
quit_button.grid(row=4, columnspan=2, sticky=(E, W))
window.mainloop()
Also as a separate question,I'm having a hard time understanding mainloop.I know that it makes the program go in an infinate loop, but from where? I don't see any logical evidence that shows that my code is on an endless loop.
Thanks so much
Here's a solution for you:
import tkinter as tk
from tkinter import E, W
window = tk.Tk()
window.title("Converter")
answer_label = tk.Label(window, text="No Answer")
answer_label.grid(row=5)
def feet_to_inches_function():
answer_label.configure(text='As you want (INCHES)')
def inches_to_feet_function():
answer_label.configure(text='As you want (FEET)')
title_label = tk.Label(window, text="Convert")
entry_bar = tk.Entry(window, font=('HELVETICA', 10))
fti_button = tk.Button(window, text="Feet to Inches", command=feet_to_inches_function)
itf_button = tk.Button(window, text="Inches to Feet", command=inches_to_feet_function)
quit_button = tk.Button(window, text="Quit", command=window.destroy)
title_label.grid(row=0, column=0, sticky=W)
entry_bar.grid(row=1, columnspan=2, sticky=(E, W))
fti_button.grid(row=3, column=0)
itf_button.grid(row=3, column=1)
quit_button.grid(row=4, columnspan=2, sticky=(E, W))
window.mainloop()
All you need is to configure the text of the Label in both functions.
*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 have a TKinter messagebox like the one below. I would like to change part of the message to a different color. For example in the messagebox below I would like the language to be Blue. Is this possible?
It's not possible to change such options of Tkinter Standard Dialogs. You need to create your own dialog. You'll also need to separate the text parts. I've tried to make something similar in the image that the OP has posted above:
from tkinter import *
root = Tk()
def choosefunc(option):
if option == "cancel":
print("Cancel choosen")
else:
print("OK choosen")
def popupfunc():
tl = Toplevel(root)
tl.title("Languages")
frame = Frame(tl)
frame.grid()
canvas = Canvas(frame, width=100, height=130)
canvas.grid(row=1, column=0)
imgvar = PhotoImage(file="pyrocket.png")
canvas.create_image(50,70, image=imgvar)
canvas.image = imgvar
msgbody1 = Label(frame, text="The", font=("Times New Roman", 20, "bold"))
msgbody1.grid(row=1, column=1, sticky=N)
lang = Label(frame, text="language(s)", font=("Times New Roman", 20, "bold"), fg='blue')
lang.grid(row=1, column=2, sticky=N)
msgbody2 = Label(frame, text="of this country is: Arabic", font=("Times New Roman", 20, "bold"))
msgbody2.grid(row=1, column=3, sticky=N)
cancelbttn = Button(frame, text="Cancel", command=lambda: choosefunc("cancel"), width=10)
cancelbttn.grid(row=2, column=3)
okbttn = Button(frame, text="OK", command=lambda: choosefunc("ok"), width=10)
okbttn.grid(row=2, column=4)
label = Label(root, text="Click to proceed:")
label.grid()
button = Button(root, text="Click", command=popupfunc)
button.grid()
(Image URL: http://imgur.com/a/Nf75v)