*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()
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)
Basically, I am making a really basic python game for school and i am just trying to rush finish this as fast as I can, after def RNG(): After I get the button to give me a random number, how do I stop the button for it? I can't spam click it.
import tkinter
from tkinter import *
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
root = Tk( )
app = Window(root)
root.title("BlackJack Game")
root.geometry("1920x1080+0+0")
root.configure(bg='gold')
lbl: Label = tkinter.Label(root, text='Welcome to Blackjack!', fg='steelblue', bg='gold1',
font=('arial', 50, "bold"))
lbl.pack()
def open():
global top
top = Toplevel()
top.configure(bg='green')
top.geometry("1920x1080+0+0")
top.title('Game Window')
lbl_1 = Label(top, text="BlackJack\n ", font=("arial", 75, "bold"), fg="black", bg="green").pack()
root.withdraw()
def RNG():
import random
x = random.randint(2, 21)
lab = Label(top, text=x, font=("arial", 20, "bold"), fg="Black", bg='brown')
lab.pack()
global but
but = Button(top, text='test', font=("arial", 20, "bold"), fg="Black", bg='brown', command=RNG, height=7, width=18).pack()
work = Button(root, text="Press Play: ", font=("arial", 20, "bold"), fg="Black", bg='orange', command=open, ).pack()
root.mainloop()
You can do this in a few ways.
First: You can set the button's state to disabled
but = Button(top, text='test', state = DISABLED, font=("arial", 20, "bold"), fg="Black", bg='brown', command=RNG, height=7, width=18).pack()
or
but['state'] = DISABLED
Note that you should be disabling the button in the function it is calling. (for simplicity sake)
Second: You can redefine the button after and change the command it calls to '" "'
but = Button(top, text='test', state = DISABLED, font=("arial", 20, "bold"), fg="Black", bg='brown', command="", height=7, width=18).pack()
that way it cannot be spammed, it also means that when you call open() it should change the but button back to normal.
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.
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)