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()
Related
Python TKinter Mac: Buttons layout strangely and when a button is clicked it looks the same (as if it hadn't been clicked) however I didn't check to see if the button worked.
and the space is a textbook
the Mac version is: a 2017 iMac running Monterey 12.4
import tkinter as tk
window = tk.Tk()
window.geometry("500x500")
window.title("Stock Watchlist & Logger")
label = tk.Label(window, text="Tester", font=('Ariel', 18))
label.pack(padx=20, pady=20)
textbox = tk.Text(window, height=3, font=('Ariel', 16))
textbox.pack(padx=10, pady=10)
numbuttframe = tk.Frame(window)
numbuttframe.columnconfigure(0, weight=1)
yesnobuttframe = tk.Frame(window)
yesnobuttframe.columnconfigure(1, weight=1)
button1 = tk.Button(numbuttframe, text="1", font=('Arial', 18))
button1.grid(row=0, column=0, sticky=tk.W+tk.E)
button2 = tk.Button(numbuttframe, text="2", font=('Arial', 18))
button2.grid(row=0, column=1, sticky=tk.W+tk.E)
button3 = tk.Button(numbuttframe, text="3", font=('Arial', 18))
button3.grid(row=0, column=2, sticky=tk.W+tk.E)
button4 = tk.Button(numbuttframe, text="4", font=('Arial', 18))
button4.grid(row=0, column=3, sticky=tk.W+tk.E)
button5 = tk.Button(numbuttframe, text="5", font=('Arial', 18))
button5.grid(row=0, column=4, sticky=tk.W+tk.E)
button6 = tk.Button(numbuttframe, text="6", font=('Arial', 18))
button6.grid(row=0, column=5, sticky=tk.W+tk.E)
yesbutton = tk.Button(yesnobuttframe, text="Yes", font=('Arial', 18))
yesbutton.grid(row=0, column=0, sticky=tk.W+tk.E)
nobutton = tk.Button(yesnobuttframe, text="No", font=('Arial', 18))
nobutton.grid(row=0, column=1, sticky=tk.W+tk.E)
numbuttframe.pack(fill='x')
yesnobuttframe.pack(fill='x')
if button1:
print("CELEBRATE!")
window.mainloop()
You set the weight to two cells weight=1 , so they expanded, since the rest have a default weight of 0. Since I don’t see your grid - the buttons are placed in frames one after the other, suggest using the pack method. And shortened your code a bit. The buttons won't work yet, because they don't have a command = call. Complex variable names in Python are usually written with an underscore.
import tkinter as tk
FONT = ('Ariel', 18)
window = tk.Tk()
window.geometry("500x500")
window.title("Stock Watchlist & Logger")
lbl = tk.Label(window, text="Tester", font=FONT)
lbl.pack(padx=20, pady=20)
text_box = tk.Text(window, height=3, font=('Ariel', 16))
text_box.pack(padx=10, pady=10)
num_btn = tk.Frame(window)
yes_no_btn = tk.Frame(window)
num_btn.pack(fill='x')
yes_no_btn.pack(fill='x')
buttons = []
for i in range(1, 7):
btn = tk.Button(num_btn, text=f"{i}", font=FONT)
btn.pack(side="left", fill='x', expand=True)
buttons.append(btn)
yes_btn = tk.Button(yes_no_btn, text="Yes", font=FONT)
yes_btn.pack(side="left", fill='x', expand=True)
no_btn = tk.Button(yes_no_btn, text="No", font=FONT)
no_btn.pack(side="left", fill='x', expand=True)
window.mainloop()
The purpose is to pull a word from the list randomly and display it on the screen in the GUI. The words are displayed in the terminal when I click the button "dammi" but I cannot get them to display in the GUI. I have tried both an Entry and Label with no success.
from tkinter import *
import random
# Window
root = Tk()
root.geometry("400x350")
root.title("Passato Remoto")
root.configure(bg="#000000")
root.resizable(False, False)
# Find Verb
verbi = ['Dare', 'Dire', 'Fare', 'Sapere', 'Prendere']
# Dammi Button
def give():
print(random.choice(verbi))
# Create Buttons
dammi = Button(root, text='Dammi un verbo',
bg='#ffffff',
fg='#000000',
borderwidth=0,
highlightthickness=0,
font=('Helvetica', 16),
height=2,
width=10,
command=give)
dammi.grid(column=0, row=2, pady=50, padx=25)
con = Button(root, text='Coniugazione',
bg='#ffffff',
fg='#000000',
borderwidth=0,
highlightthickness=0,
font=('Helvetica', 16),
height=2,
width=10)
con.grid(column=2, row=2, pady=50, padx=25)
# Put Verb On Screen
verb = Entry(root, text=give(), font=('Helvetica', 40), width=10, bg="#ffffff", fg="#000000")
verb.grid(column=0, columnspan=3, row=1, pady=50, padx=80)
root.mainloop()
The easiest way is probably to use a StringVar. I've added this into the question code.
from tkinter import *
import random
root = Tk()
root.geometry("400x350")
root.title("Passato Remoto")
root.configure(bg="#000000")
root.resizable(False, False)
# Find Verb
verbi = ['Dare', 'Dire', 'Fare', 'Sapere', 'Prendere']
# Dammi Button
sample = StringVar() # Added
def give(): # Changed
sample.set( random.choice(verbi) )
print( sample.get() )
# Create Buttons
dammi = Button(root, text='Dammi un verbo',
bg='#ffffff',
fg='#000000',
borderwidth=0,
highlightthickness=0,
font=('Helvetica', 16),
height=2,
width=10,
command=give)
dammi.grid(column=0, row=2, pady=50, padx=25)
con = Button(root, text='Coniugazione',
bg='#ffffff',
fg='#000000',
borderwidth=0,
highlightthickness=0,
font=('Helvetica', 16),
height=2,
width=10)
con.grid(column=2, row=2, pady=50, padx=25)
# Put Verb On Screen
# changed here
verb = Label(root, font=('Helvetica', 40), textvariable = sample, width=10, bg="#ffffff", fg="#000000")
verb.grid(column=0, columnspan=3, row=1, pady=50, padx=80)
give()
root.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()
I have a program called main.py:
def prog2():
exec(open("prog2.py").read())
buttonHMI = tk.Button(text="Program HMI", width=15, font=("Arial", 14), command=prog2)
buttonHMI.grid(column=0, row=3, columnspan=1, padx=20, pady=10, sticky=W)
So there's 1 button and when pressed it executes a 2nd program called prog2:
from tkinter import *
from tkinter import ttk
window = tk.Tk()
def close():
window.destroy
CB1 = Checkbutton(window, text="Enable VNC", font=("Arial", 12), variable=action_VNC)
CB1.grid(row=0, sticky=W, padx=10)
CB1.configure(bg='#34aeeb')
ButtonClose= tk.Button(window, text="Close", width=20, font=("Arial", 14), command=window.destroy)
ButtonClose.grid(columnspan=2, column = 3, row=9, sticky=W, padx=10, pady=10)
class Timer:
def __init__(self, window=None):
self.sv = tk.StringVar()
ButtonShow = tk.Button(window, text="Program HMI", width=20, font=("Arial", 14), command=self.actions)
ButtonShow.grid(columnspan=2, row=9, column = 0, sticky=W, padx=10, pady=10)
progress = ttk.Progressbar(window, orient = HORIZONTAL, length = 500, mode = 'determinate')
progress.grid(row=10, columnspan = 4, sticky=W, padx=10, pady=10)
outText = Text(window, width=50, height=20, wrap=WORD)
outText.grid(row=11, columnspan=4, padx=10)
###########################################################
#start actions.
def actions(self):
print("action")
Timer()
window.mainloop()
The problem I'm having is from main.py, I open prog2, some of the objects (Buttonshow, progress, outText) will show up in the main.py window. But CB1 and Close objects will show in prog2. If I execute prog2.py by itself, then all the objects will show correctly in prog2 window. How do I get all of prog2 objects to show in prog2?
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)