I have two python files - main.py and dictionary.py . I am creating an 8 in 1 app. When I run dictionary.py, I can get the meaning of the desired word but while running main.py, the value of the variables gets lost.
While running main.py, the value of text_v gets reset. But while running only dictionary.py, the value of text_v remains the same. I don't know why it is happening. Please help
main.py
import tkinter as tk
from PIL import ImageTk, Image
import dictionary as dc
win = tk.Tk()
win.geometry("1280x720+100-100")
calc_icon = Image.open("calculator_icon.png")
calc_icon = ImageTk.PhotoImage(calc_icon)
print(type(calc_icon))
calc_button = tk.Button(win, image=calc_icon, borderwidth=0)
calc_button.place(x=160, y=180, anchor='center')
timetable_icon = Image.open("timetable_icon.png")
timetable_icon = ImageTk.PhotoImage(timetable_icon)
timetable_button = tk.Button(win, image=timetable_icon, borderwidth=0)
timetable_button.place(x=480, y=180, anchor='center')
texteditor_icon = Image.open("texteditor_icon.png")
texteditor_icon = ImageTk.PhotoImage(texteditor_icon)
texteditor_button = tk.Button(win, image=texteditor_icon, borderwidth=0)
texteditor_button.place(x=800, y=180, anchor='center')
dictionary_icon = Image.open("dictionary.png")
dictionary_icon = ImageTk.PhotoImage(dictionary_icon)
dictionary_button = tk.Button(win, image=dictionary_icon, borderwidth=0, command=dc.dic)
dictionary_button.place(x=1120, y=180, anchor='center')
budget_icon = Image.open("budgetmanager.png")
budget_icon = ImageTk.PhotoImage(budget_icon)
budget_button = tk.Button(win, image=budget_icon, borderwidth=0)
budget_button.place(x=160, y=450, anchor='center')
todo_icon = Image.open("todo.png")
todo_icon = ImageTk.PhotoImage(todo_icon)
todo_button = tk.Button(win, image=todo_icon, borderwidth=0)
todo_button.place(x=480, y=450, anchor='center')
bookmark_icon = Image.open("bookmark.png")
bookmark_icon = ImageTk.PhotoImage(bookmark_icon)
bookmark_button = tk.Button(win, image=bookmark_icon, borderwidth=0)
bookmark_button.place(x=800, y=450, anchor='center')
news_icon = Image.open("news.png")
news_icon = ImageTk.PhotoImage(news_icon)
news_button = tk.Button(win, image=news_icon, borderwidth=0)
news_button.place(x=1120, y=450, anchor='center')
win.mainloop()
dictionary.py
import tkinter as tk
import PyDictionary as pd
def dic():
def click():
m_canvas = tk.Canvas(win)
print('line 12', text_v)
word = text_v.get()
print(word)
m_canvas.place(x=300, y=0)
m_label = tk.Label(m_canvas, text='Meaning', font=('Callibri', 20))
m_label.grid(row=0, column=0)
translated = pd.PyDictionary.meaning(word)
print(translated)
meaning = tk.Label(m_canvas, text=translated, font=('Callibri', 15))
meaning.grid(row=1, column=0)
win = tk.Tk()
win.title('Dictionary')
win.geometry('1280x720+100-100')
text_v = tk.StringVar()
print('line 31', text_v)
canvas = tk.Canvas(win)
canvas.place(x=0, y=0, anchor='nw')
head = tk.Label(canvas, text='Dictionary:', font=('Callibri, 20'))
head.grid(row=0, column=0)
lab = tk.Label(canvas, text='Enter a word', font=('Callibri', 20))
lab.grid(row=1, column=0)
inp = tk.Entry(canvas, textvariable=text_v)
inp.grid(row=2, column=0)
lab2 = tk.Label(canvas, text='')
lab2.grid(row=3, column=0)
btn = tk.Button(canvas, text='Submit', command=click)
btn.grid(row=4, column=0)
win.mainloop()
Related
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()
This is supposed to be a pet company administration GUI app made with tkinter
I am following a tutorial, I don't know why I'm getting this error but the tutor isn't getting it.
I have tried changing the string to int but it's still giving me an error
The program runs when the database is empty, I get this error only when I have data in my database
main.py
from tkinter import *
from tkinter import ttk
from ttkbootstrap import Style
import time
from rex_database import DBConnect
from tkinter import messagebox
dbConnect = DBConnect()
root = Tk()
x = root.winfo_screenwidth()
y = root.winfo_screenheight()
root.geometry("%dx%d" % (x, y))
notebook = ttk.Notebook(root, height=y, width=x)
root.title("Rex Inc System")
style = Style(theme='solar') # bootstrap theme
# tabs
tab1 = Frame(notebook)
tab2 = Frame(notebook)
tab3 = Frame(notebook)
tab4 = Frame(notebook)
tab5 = Frame(notebook)
tab6 = Frame(notebook)
tab7 = Frame(notebook)
# naming tabs
notebook.add(tab1, text="Inventory")
notebook.add(tab2, text="Sales System")
notebook.add(tab3, text="Register Pet")
notebook.add(tab4, text="Adopt Pet")
notebook.add(tab5, text="Studding")
notebook.add(tab6, text="Database")
notebook.add(tab7, text="Calculators")
notebook.grid()
# tab1 content
Label(tab1, text="hbthryhy", fg='green', bg='black', relief="solid", font=("arial", 17, "bold")).grid(row=0, column=0)
# tab2 content
Label(tab2, text="nunujny", fg='blue', bg='black', relief="solid", font=("arial", 17, "bold")).grid(row=0, column=0)
# tab3 content
ttk.Label(tab3, text='Pet Name:').grid(row=0, column=0, pady=10)
petname = ttk.Entry(tab3, width=30, font=('Arial', 10))
petname.grid(row=0, column=1, columnspan=2)
SpanType = StringVar()
SpanType.set("Dog")
ttk.Label(tab3, text='Pet Type:').grid(row=1, column=0)
ttk.Radiobutton(tab3, text="Dog", variable=SpanType, value="Dog").grid(row=1, column=1)
ttk.Radiobutton(tab3, text="Cat", variable=SpanType, value="Cat").grid(row=1, column=2)
SpanType = StringVar()
SpanType.set("Male")
ttk.Label(tab3, text='Gender:').grid(row=2, column=0)
ttk.Radiobutton(tab3, text="Male", variable=SpanType, value="Male").grid(row=1, column=1)
ttk.Radiobutton(tab3, text="Female", variable=SpanType, value="Female").grid(row=1, column=2)
ttk.Label(tab3, text='Pet Description:').grid(row=2, column=0)
pet_desc = ttk.Entry(tab3, width=50)
pet_desc.grid(row=2, column=1)
save_reg = ttk.Button(tab3, text="Save", style='success.TButton')
save_reg.grid(row=3, column=1)
def save_click():
print("Pet Name is {}".format(petname.get()))
print("Pet type is {}".format(SpanType.get()))
print("Pet Description: {}".format(pet_desc.get()))
dbsave = dbConnect.Add(Petname=petname.get(), Type=SpanType.get(), Description=pet_desc.get())
messagebox.showinfo(title="Success", message=dbsave)
petname.delete(0, 'end')
pet_desc.delete(0, 'end')
save_reg.config(command=save_click)
date_reg = time.asctime(time.localtime(time.time()))
Label(tab3, text=date_reg).grid(row=4, column=1)
# tab4 content
Label(tab4, text="grgrgrg", fg='blue', bg='black', relief="solid", font=("arial", 17, "bold")).grid(row=0, column=0)
# tab5 content
Label(tab5, text="thtthhth", fg='blue', bg='black', relief="solid", font=("arial", 17, "bold")).grid(row=0, column=0)
# tab6 content
tv = ttk.Treeview(tab6)
tv.pack()
tv.heading("#0", text="ID")
tv.configure(column=('Petname', 'Type', 'Description'))
tv.heading("Petname", text="Pet Name")
tv.heading("Type", text="Type")
tv.heading("Description", text="Description")
listing = dbConnect.Listpets()
for row in listing:
tv.insert('', 'end', '#{}'.format(row["ID"]), text=row["ID"])
tv.set('#{}'.format(row["ID"]), 'Petname', row["Petname"])
tv.set('#{}'.format(row["ID"]), 'Type', row["Type"])
tv.set('#{}'.format(row["ID"]), 'Description', row["Description"])
# tab7 content
# simple calculator
display = ttk.Entry(tab7, width=77).grid(row=0, column=0, columnspan=2)
root.state("zoomed")
root.mainloop()
I want to change this label so I used the widget.config option but it's not working for some reason. Here's my code:
import tkinter as tk
root = tk.Tk()
root.attributes('-fullscreen', True)
exit_button = tk.Button(root, text="Exit", command = root.destroy)
exit_button.place(x=1506, y=0)
def answer():
global main_entry
answer_label.config(main_entry)
frame = tk.Frame(root)
main_entry = tk.Entry(frame, width=100)
main_entry.grid(row=0, column=0)
go_button = tk.Button(frame, text='Go!', width=85, command = answer)
go_button.grid(row=1, column=0)
answer_label = tk.Label(text = "Hey").pack()
frame.place(relx=.5, rely=.5, anchor='center')
root.mainloop()
While using the config function, you have to mention what is it that you want to change.
try:-
answer_label.config(text="Text that you want to be displayed")
Also you have not fetched the value from the Entry widget: For that, you can use:
answer_label.config(text=main_entry.get())
Full code will look like this:
import tkinter as tk
root = tk.Tk()
root.attributes('-fullscreen', True)
exit_button = tk.Button(root, text="Exit", command = root.destroy)
exit_button.place(x=1506, y=0)
def answer():
answer_label.config(text=main_entry.get())
frame = tk.Frame(root)
main_entry = tk.Entry(frame, width=100)
main_entry.grid(row=0, column=0)
go_button = tk.Button(frame, text='Go!', width=85, command = answer)
go_button.grid(row=1, column=0)
answer_label = tk.Label(text = "Hey")
answer_label.pack()
frame.place(relx=.5, rely=.5, anchor='center')
root.mainloop()
This code works. It configures the label to change the text. I don't know what you are trying to achieve with the config(main_entry).
import tkinter as tk
root = tk.Tk()
root.attributes('-fullscreen', True)
exit_button = tk.Button(root, text="Exit", command = root.destroy)
exit_button.place(x=1506, y=0)
def answer():
global main_entry, answer_label
answer_label.config(text="hi")
frame = tk.Frame(root)
main_entry = tk.Entry(frame, width=100)
main_entry.grid(row=0, column=0)
go_button = tk.Button(frame, text='Go!', width=85, command = answer)
go_button.grid(row=1, column=0)
answer_label = tk.Label(text = "Hey")
answer_label.pack()
frame.place(relx=.5, rely=.5, anchor='center')
root.mainloop()
I am a novice and working with Tkinter for the first time. My project is an "I Spy" book which moves picture to picture utilizing buttons. I like the look of the LabelFrame widget and want to use the title for the text that corresponds with each image. I am able to achieve the images updating correctly but the title remains the same. I have tried config, forgetting and then rebuilding the frame, and I think something else which I can't recall and none of that has worked. I have searched online, reviewed Stack Overflow similar questions--which are very few and lead me to believe that this cannot be done. Thank you for your assistance.
from tkinter import *
from tkinter import Button
from tkinter import Label
from PIL import ImageTk
from PIL import Image
root = Tk()
root.title('')
root.attributes('-toolwindow', True)
root.geometry('620x660+100+0')
img2 = ImageTk.PhotoImage(Image.open('spy_images/rainbow.jpg'))
img4 = ImageTk.PhotoImage(Image.open('spy_images/pods.jpg'))
img5 = ImageTk.PhotoImage(Image.open('spy_images/lion.jpg'))
img6 = ImageTk.PhotoImage(Image.open('spy_images/bike.jpg'))
img7 = ImageTk.PhotoImage(Image.open('spy_images/binary.jpg'))
image_list = [img2, img4, img5, img6, img7]
text2 = 'A rainbow, not in the sky!'
text4 = 'Dangly, weird seed pods.'
text5 = 'A stoney grin.'
text6 = 'A lane just for bikes.'
text7 = 'A different way to count.'
text_list = [text2, text4, text5, text6, text7]
make_frame = LabelFrame(root, text='A rainbow, not in the sky!', width=100, height=100,
font=('Arial', 14, 'bold'), fg='red', bd=10)
make_frame.grid(row=0, column=1, columnspan=5)
img_filename = 'spy_images/rainbow.jpg'
PIL_image = Image.open(img_filename)
img = ImageTk.PhotoImage(PIL_image)
in_frame = Label(make_frame, image=img)
in_frame.grid(padx=10, pady=10)
def forward(image_num, text_num):
global make_frame
global in_frame
global button_forward
global button_back
in_frame.grid_forget()
in_frame = Label(image=image_list[image_num])
button_forward = Button(root, text='>>', command=lambda:
forward(image_num+1, text_num+1))
button_back = Button(root, text='<<', command=lambda:
back(image_num-1, text_num-1))
if image_num == 7:
button_forward = Button(root, text='>>', state=DISABLED)
make_frame.grid(row=0, column=1, columnspan=5)
in_frame.grid(row=0, column=0, columnspan=5)
in_frame.grid(padx=10, pady=10)
button_forward.grid(row=1, column=5)
button_back.grid(row=1, column=1)
button_back.grid_columnconfigure(0, weight=1)
button_back.grid_columnconfigure(2, weight=1)
button_back.grid_columnconfigure(4, weight=1)
def back(image_num, text_num):
global make_frame
global in_frame
global button_forward
global button_back
in_frame.grid_forget()
in_frame = Label(image=image_list[image_num - 1])
button_forward = Button(root, text='>>', command=lambda:
forward(image_num + 1, text_num + 1))
button_back = Button(root, text='<<', command=lambda:
back(image_num - 1, text_num - 1))
if image_num == 1:
button_back = Button(root, text='<<', state=DISABLED)
make_frame.grid(row=0, column=1, columnspan=5)
in_frame.grid(row=0, column=0, columnspan=3)
in_frame.grid(padx=10, pady=10)
button_forward.grid(row=1, column=5)
button_back.grid(row=1, column=1)
button_back.grid_columnconfigure(0, weight=1)
button_back.grid_columnconfigure(2, weight=1)
button_back.grid_columnconfigure(4, weight=1)
button_back = Button(root, text='<<', command=back, state=DISABLED, bg='#d9d5d4',
font=('Arial', 14, 'bold'))
button_exit = Button(root, text='Cancel', command=root.quit, bg='#d9d5d4', font=('Arial', 12))
button_forward = Button(root, text='>>', command=lambda: forward(2, 2), bg='#d9d5d4', font=('Arial', 14, 'bold'))
button_back.grid(row=1, column=1)
button_exit.grid(row=1, column=3)
button_forward.grid(row=1, column=5)
button_back.grid_columnconfigure(0, weight=1)
button_back.grid_columnconfigure(2, weight=1)
button_back.grid_columnconfigure(4, weight=1)
root.mainloop()
If you have some questions about the exampel I'll try to answer you.
import tkinter as tk
#you dont need this but if you want to cycle, wich is would be nice there you go
from itertools import cycle
#create a list with strings you like to display
li = ['A rainbow, not in the sky!','Dangly, weird seed pods.','A stoney grin.',
'A lane just for bikes.','A different way to count.']
#here we create a cycle of that list
my_cycled_li = cycle(li)
#the change function
def change():
#set var to next element in list
var.set(next(my_cycled_li))
#update the LabelFrame
lf.configure(text=var.get())
root = tk.Tk()
#variable to change
var = tk.StringVar()
#there can be a default setting
var.set('default')
lf = tk.LabelFrame(root,text=var.get(),width=200,height=100,bg='red')
#you dont need this, this means the Frame size isnt the size of the widget it contains.
lf.pack_propagate(0)
lf.pack()
b = tk.Button(lf,text='change', command=change)
b.pack()
root.mainloop()
hoped it helps.
I'm making a wrong-word corrector so I use replace method, but it doesn't work
because it is not all same word.
For example:
string = i like icecream
I want to change the word = icecream
It only works for "i like icecream" if it is all the same
This is my whole code:
# coding: utf-8
from tkinter import *
import tkinter.messagebox
root=Tk()
root.title("words corrector")
root.resizable(0, 0)
root.geometry("+{}+{}".format(600, 400))
mainFrame = Frame(root, width=600, height=400)
mainFrame.pack()
textFrame = Frame(mainFrame, width=100, height=100)
textFrame.pack()
textFrame_1 = Frame(mainFrame, width=100, height=100)
textFrame_1.pack()
textFrame_2 = Frame(mainFrame,width=100, height=100)
textFrame_2.pack()
scrollbar = Scrollbar(textFrame)
scrollbar.pack(side=RIGHT, fill="y")
#textField == sentance
textField = Text(textFrame, width=50, height=10, bd=5, relief="groove")
textField.insert(CURRENT,"enter the text\n")
textField.pack(side=LEFT, padx=(5, 0), pady=(5, 5))
textField["yscrollcommand"] = scrollbar.set
#textField_2 == wrong word
textField_2= Text(textFrame_1, width=15, height=3, bd=5, relief="groove")
textField_2.insert(CURRENT,"wrong words\n")
textField_2.pack(side=LEFT, padx=(5,0), pady=(5,5))
#textField_3 == correct word
textField_3= Text(textFrame_1,width=15, height=3, bd=5, relief="groove")
textField_3.insert(CURRENT, "correct words\n")
textField_3.pack(side=LEFT, padx=(5,0), pady=(5,5))
def chg():
sentance = textField.get("1.0",END)
wrong_word = textField_2.get("1.0",END)
correct_word = textField_3.get("1.0",END)
result = sentance.replace(wrong_word,correct_word)
textField_4.insert("1.0",result)
def msg():
tkinter.messagebox.showerror("error","there's no words")
def ok():
if textField_2.get("1.0",END) in textField.get("1.0",END):
chg()
else:
msg()
okButton = Button(textFrame_1, text="OK", command=ok)
okButton.pack(padx=40, pady=(20,5))
scrollbar_2 = Scrollbar(textFrame_2)
scrollbar_2.pack(side=RIGHT, fill="y")
textField_4 = Text(textFrame_2, width=50, height=10, bd=5, relief="groove")
textField_4.pack(side=LEFT, padx=(5, 0), pady=(5, 5))
textField_4["yscrollcommand"] = scrollbar.set
root.mainloop()
Try the following code. You have to convert unicode characters to a string and use str.replace.
from tkinter import *
import tkinter.messagebox
root=Tk()
root.title("words corrector")
root.resizable(0, 0)
root.geometry("+{}+{}".format(600, 400))
mainFrame = Frame(root, width=600, height=400)
mainFrame.pack()
textFrame = Frame(mainFrame, width=100, height=100)
textFrame.pack()
textFrame_1 = Frame(mainFrame, width=100, height=100)
textFrame_1.pack()
textFrame_2 = Frame(mainFrame,width=100, height=100)
textFrame_2.pack()
scrollbar = Scrollbar(textFrame)
scrollbar.pack(side=RIGHT, fill="y")
#textField == sentance
textField = Text(textFrame, width=50, height=10, bd=5,
relief="groove")
textField.insert(CURRENT,"enter the text\n")
textField.pack(side=LEFT, padx=(5, 0), pady=(5, 5))
textField["yscrollcommand"] = scrollbar.set
#textField_2 == wrong word
textField_2= Text(textFrame_1, width=15, height=3, bd=5,
relief="groove")
textField_2.insert(CURRENT,"wrong words\n")
textField_2.pack(side=LEFT, padx=(5,0), pady=(5,5))
#textField_3 == correct word
textField_3= Text(textFrame_1,width=15, height=3, bd=5,
relief="groove")
textField_3.insert(CURRENT, "correct words\n")
textField_3.pack(side=LEFT, padx=(5,0), pady=(5,5))
scrollbar_2 = Scrollbar(textFrame_2)
scrollbar_2.pack(side=RIGHT, fill="y")
textField_4 = Text(textFrame_2, width=50, height=10, bd=5,
relief="groove")
textField_4.pack(side=LEFT, padx=(5, 0), pady=(5, 5))
textField_4["yscrollcommand"] = scrollbar.set
def chg():
sentance = textField.get("1.0",END)
wrong_word = textField_2.get("1.0",END)
correct_word = textField_3.get("1.0",END)
# result = sentance.replace(wrong_word,correct_word)
result = str.replace(str(sentance), wrong_word, correct_word)
textField_4.insert("1.0",result)
def msg():
tkinter.messagebox.showerror("error","there's no words")
def ok():
# if textField_2.get("1.0",END) in textField.get("1.0",END):
chg()
# else:
# msg()
okButton = Button(textFrame_1, text="OK", command=ok)
okButton.pack(padx=40, pady=(20,5))
root.mainloop()
OUTPUT:
EDIT
If you want want to keep that "enter the text part" and type your sentence below, without replacing it, you should do the following in the relevant line for the code to work.
result = str.replace(str(sentance).replace('enter the text\n',''), wrong_word.replace('wrong words\n',''), correct_word.replace('correct words\n',''))