Related
ive been working on a duplicate image detector for a bit now, and ive run into an error that is only encountered for me, as ive seen nobody else have this error when they test it. the error is in the function showImages() where i have 3 labels that i want to use, yet only those three labels dont work. the buttons and the images work just fine, but the labels do not. i have been pulling my hair out for the past couple days trying to find what is making these 3 labels not pop up.
the code should would fine on other computers, granted you have the modules. to test, you need 2 folders with images, and some of those images should have a duplicate in its counterpart folder.
from tkinter import filedialog
import cv2, numpy, os, math
from tkinter import *
from PIL import Image, ImageTk
import tkinter as tk
root = Tk()
root.configure(background="black")
canvas = Canvas(root, bd=0, bg = 'black', width = 1920, height = 1080)
canvas.pack()
canvas.configure(bg = 'black')
file1 = ''
file2 = ''
identicalImages1 = []
identicalImages2 = []
directories = []
blacklisted = [".gif" ,".avi",".exe", ".wav", ".avf", ".mov", ".mp4", ".webm", ".flv", ".txt", ".swf", ".log", ".url"]
fonta = 'Terminal'
text_color = 'lime'
indecs = 0
leniancy = 4 #default
label1 = Label(root, bg = 'black', fg = text_color, text= '', font = (fonta, 30)) # 5 given labels
label2 = Label(root, bg = 'black', fg = text_color, text= '', font = (fonta, 30))
label3 = Label(root, bg = 'black', fg = text_color, text= '', font = (fonta, 30))
label4 = Label(root, bg = 'black', fg = text_color, text= '', font = (fonta, 30))
label5 = Label(root, bg = 'black', fg = text_color, text= '', font = (fonta, 30))
image1 = Label(root, bg = 'black', fg = text_color, font = (fonta, 30)) # 5 given images
image2 = Label(root, bg = 'black', fg = text_color, font = (fonta, 30))
image3 = Label(root, bg = 'black', fg = text_color, font = (fonta, 30))
image4 = Label(root, bg = 'black', fg = text_color, font = (fonta, 30))
image5 = Label(root, bg = 'black', fg = text_color, font = (fonta, 30))
button1 = Button(root, bg = 'black', fg = text_color, font = (fonta, 30)) # 5 given buttons
button2 = Button(root, bg = 'black', fg = text_color, font = (fonta, 30))
button3 = Button(root, bg = 'black', fg = text_color, font = (fonta, 30))
button4 = Button(root, bg = 'black', fg = text_color, font = (fonta, 30))
button5 = Button(root, bg = 'black', fg = text_color, font = (fonta, 30))
slider1 = Scale(root,bg = 'black', fg = text_color)
slider2 = Scale(root,bg = 'black', fg = text_color)
def forgor(): # resets every widget when swapping pages
try:
label1.place_forget()
label1.config(font = (fonta, 30))
label2.place_forget()
label2.config(font = (fonta, 30))
label3.place_forget()
label3.config(font = (fonta, 30))
label4.place_forget()
label4.config(font = (fonta, 30))
label5.place_forget()
label5.config(font = (fonta, 30))
button1.place_forget()
button1.config(font = (fonta, 30))
button2.place_forget()
button2.config(font = (fonta, 30))
button3.place_forget()
button3.config(font = (fonta, 30))
button4.place_forget()
button4.config(font = (fonta, 30))
button5.place_forget()
button5.config(font = (fonta, 30))
image1.place_forget()
image2.place_forget()
image3.place_forget()
image4.place_forget()
image5.place_forget()
slider1.place_forget()
slider1.place_forget()
except:
pass
def start1(): # main menu
global file1, file2
file1 = ''
file2 = ''
forgor()
label1.config(bd = 2, text = "IMAGE DUPE COMPARER")
label1.place(x = 960, y = 50, anchor= 'center') # title name
label2.config(bd = 2)
label2.place(x = 480, y = 530, anchor= 'center') # folder 1 name
label3.config(bd = 2)
label3.place(x = 1440, y = 530, anchor= 'center') # folder 2 name
button1.config(text = "folder 1", command = lambda: ask_for_folder(1)) # folder 1 button
button1.place(x = 480, y = 590, anchor= 'center')
button2.config(text = "folder 2", command = lambda: ask_for_folder(2)) # folder 2 button
button2.place(x = 1440, y = 590, anchor= 'center')
button3.config(text = "Go!", command = start2)
button3.place(x = 960, y = 700, anchor= 'center') # initiate button, dont worry about the window turning to (not responding) all its doing is the image comparing stuff
button4.config(text = "Options", command = options_menu) # options to change things
button4.place(x = 960, y = 780, anchor= 'center')
button5.config(text = "Exit", command = root.destroy) # exit button
button5.place(x = 960, y = 860, anchor= 'center')
print(leniancy)
def options_menu():
forgor() # resets all widgets
label1.config(text = "Image Leniency\n(the lower, the more accurate, while the higher, the more possible images detected)", font = (fonta, 20))
label1.place(x = 960, y = 100, anchor= 'center')
slider1.set(leniancy) # to set it to what was previously set, or the default
slider1.config(bd = 0 ,from_ = 1, to = 10, tickinterval = 1, variable = leniancy, length = 400, orient = HORIZONTAL, font = (fonta, 20), command = lenancy)
slider1.place(x = 960, y = 170, anchor= 'center')
button1.config(text = "Save & Exit", command = start1) #back to main menu
button1.place(x = 960, y = 860, anchor= 'center')
def start2(): # second part of the code [BUG] when you click the go button without specifying folders, it breaks. its an easy fix but thats not at the top of my list right now
global folder1, folder2
forgor()
folder1 = get_average_colors(file1)
folder2 = get_average_colors(file2)
for i in folder1.keys():
for j in folder2.keys():
compare_images(i, j)
showImages(identicalImages1[indecs], identicalImages2[indecs])
def ask_for_folder(folder):
global file1, file2
if folder == 1:
try:
file1 = filedialog.askdirectory().replace("\\", "/")
label2.config(text= file1[[i for i, c in enumerate(file1) if c == "/"][-1] + 1:])
except IndexError:
label2.config(text= '')
elif folder == 2:
try:
file2 = filedialog.askdirectory().replace("\\", "/")
label3.config(text= file2[[i for i, c in enumerate(file2) if c == "/"][-1] + 1:])
except IndexError:
label3.config(text= '')
def lenancy(value): # used in the menu
global leniancy
leniancy = value
def swag_sort(stri): # used for sorting images that were all saved as the same name, with the ending being an index of those images, so like: image (1).jpg
try:
return int(stri[stri.index('(') + 1:stri.index(')')])
except:
return stri
def remove_non_ascii(s): # removes all the weird characters that arent gonna work
try:
return "".join(c for c in s if ord(c)<128)
except:
return s
def tell_if_non_ascii(s):
for i in s:
if ord(i)>128:
return True
else:
return False
def round(x, interval): # the main way to change the leniency of image checking, for stuff like image corruption
return (-x % interval + x)
def get_average_colors(currentDirectory): # gets every image in the folder, divides those images by 4, then gets the average rgb color for the 4 quadrant images. the 4 quadrant stuff is usually to help greyscale images
global directories
average = {}
os.chdir(currentDirectory)
directories.append(currentDirectory)
list1 = [i for i in [i for i in [i for i in os.listdir(currentDirectory) if "." in i]] if i[i.index('.'):] not in blacklisted] # removes shortcuts and folders from the list of images
Placeholder = {}
for i in list1:
if tell_if_non_ascii(i):
Placeholder[i] = remove_non_ascii(i)
for i in Placeholder.keys():
os.rename(i,Placeholder[i])
list1 = [remove_non_ascii(i) for i in list1]
try:
list1 = sorted(list1, key=swag_sort)
except:
pass
for image in list1: # splits every image in the list into 4 parts, then gets the average pixel of each
print(image, end=" \r")
myimg = cv2.imread(image)
(h, w) = myimg.shape[:2]
(cX, cY) = (int(w) // 2, int(h) // 2)
imgs = [myimg[0:cY, 0:cX],myimg[0:cY, cX:w],myimg[cY:h, 0:cX],myimg[cY:h, cX:w]] # goes from top left, to top right, then bottom left, then bottom right
avg_color = []
for i in imgs:
avg_color_per_row = numpy.average(i, axis=0)
avg_color.append([round(i, leniancy) for i in numpy.average(avg_color_per_row, axis=0)])
average[image] = avg_color
return average
def compare_images(image1, image2): # image comparison algorithm
global identicalImages1, identicalImages2
list1 = folder1[image1]
list2 = folder2[image2]
p = []
for i, j in zip(list1, list2):
d = math.sqrt((i[0] - j[0])**2 + (i[1] - j[1])**2 + (i[2] - j[2])**2)
p.append(int((1 - d / (255 * math.sqrt(3))) * 100)) # stinky math, basically simplified version of a whole bunch of processes in one to get the percentage of how off it is
#if set(p) == {100}: #for correctness and nothing else
if p.count(100) >= 4: #1 should be changeable, and should be a weight if i ever am gonna find out how to correctly make an ai
identicalImages1.append(image1)
identicalImages2.append(image2)
print(str(p) + "100% chance of {} being a duplicate to {}".format(image1, image2))
else:
print(str(p) + "% chance of {} being a duplicate to {}".format(image1, image2), end=" \r")
return p
def showImages(img1, img2): # tkinter part that lets you compare images
forgor()
imag1 = (Image.open(directories[0] + '/' + img1))
imag2 = (Image.open(directories[1] + '/' + img2))
resized_image1 = imag1.resize((960, 540), Image.ANTIALIAS)
resized_image2 = imag2.resize((960, 540), Image.ANTIALIAS)
new_image1 = ImageTk.PhotoImage(resized_image1)
new_image2 = ImageTk.PhotoImage(resized_image2)
ratio = str(identicalImages1.index(img1)) + "/" + str(len(identicalImages1))
image1.place(x = 0, y = 20)
image1.config(image= new_image1) # image 1
image1.image = new_image1
image2.place(x = 960, y = 20)
image2.config(image= new_image2) # image 2
image2.image = new_image2
label1.place(x = 0, y = 20)
label1.config(font=(fonta, 20), text= str(img1)) #image 1 name, displayed in the top left corner of image 1
label2.place(x = 960, y = 20)
label2.config(font=(fonta, 20), text= str(img2)) #image 2 name, displayed in the top left corner of image 2
label3.place(x = 960, y = 530, anchor= 'center') # displays how far you are through the grading process
label3.config(font = (fonta, 20), text = ratio)
button1.place(x = 480, y = 590, anchor= 'center')
button1.config(font=(fonta, 20), text= "delete {}".format(str(img1)), command= lambda: destroyed_image(img1, directories[0], replaced())) # destroys the image1
button2.place(x = 1440, y = 590, anchor= 'center')
button2.config(font=(fonta, 20), text= "delete {}".format(str(img2)), command= lambda: destroyed_image(img2, directories[1], replaced())) # destroys the image2
button3.place(x = 960, y = 590, anchor= 'center')
button3.config(text = "pass", font=(fonta, 20), command= replaced) # passes both images
button4.place(x = 960, y = 670, anchor= 'center')
button4.config(text = "delete both", font=(fonta, 20), command= lambda: destroyed_image(img1, directories[0], destroyed_image(img2, directories[1], replaced()))) # runs the destroy twice with an, in my opinion, genius way to incorperate both and still replace the images
def replaced(): # when done with it
global indecs
indecs += 1
try:
showImages(identicalImages1[indecs], identicalImages2[indecs])
except IndexError: # when at the end of the list
start1()
def destroyed_image(imag, directory, diff): # destroys all the images that are detected simular ahead of it, if it was chosen to be deleted WORK IN PROGRESS
try:
for i in [index for index, element in enumerate(identicalImages1) if element == imag][1:]:
identicalImages1.pop(i)
identicalImages2.pop(i)
except:
try:
for i in [index for index, element in enumerate(identicalImages2) if element == imag][1:]:
identicalImages1.pop(i)
identicalImages2.pop(i)
except:
pass
try:
os.remove(directory + '/' + imag)
except:
print("image already deleted or doesnt exist")
diff
start1()
try:
mainloop()
except:
pass
i found the answer just a bit ago, apparently no matter the order you place it in, which ever widget was defined first goes on the bottom, and i put my labels to be defined first. so, the labels were, no matter what, being placed behind the image, and the image was resized so it still had the dimensions of the original image, just the pixels of the image were resized. so they would hide underneath the images no matter what.
My first post, so I apologize as I learn to use this. Also, I am sure my code is very jumbled and rudimentary, but I am not here to get someone to re-write my code. I have a question about how a widget is behaving and an error code I am getting.
I currently have two frames in a mainWindow, and when you click a button labeled "New", a small window (newWindow) pops up that has a registration form, and a button labeled "Register" to write the form data to a .txt file.
The problem I am having is that if I do not define where the button should go, it appears in the mainWindow. If I specify for it to appear in the newWindow, it does not appear and throws me the following error:
File
"/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/init.py",
line 1507, in nametowidget
w = w.children[n] KeyError: '!button2'
Here is my code (get ready for a mess!):
from tkinter import *
from tkinter import ttk
from time import strftime
from tkmacosx import SFrame, Button
import tkinter as tk
mainWindow = Tk()
mainWindow.title('BATCO Main')
def createWindow():
#When createWindow() is called, the following code is executed, starting with creating the window object itself, which is set to the Tk() widget caller
newWindow = Tk()
# Add title of window
newWindow.title('Patron Entry')
# Set from and geometry of the Button (widthxheight+XPOS+YPOS)
newWindow.geometry('500x350+100+200')
# THIS PART CREATES AND APPENDS THE FILE FOR STORING NAMES:
def saveInfo():
# Defines Variables(3) for the "get" Method that will take from the StringVar Variables(2) above:
fname_info = fname.get()
mname_info = mname.get()
lname_info = lname.get()
suffix_info = suffix.get()
addr1_info = addr1.get()
addr2_info = addr2.get()
city_info = city.get()
state_info = state.get()
zipcode_info = zipcode.get()
print(fname_info, mname_info, lname_info, suffix_info, addr1_info, addr2_info, city_info, state_info, zipcode_info)
# This starts by opening the file 'patrons.txt' and assigns it to the file Variable. It uses the Arg 'a' which stands for Append, so it will add to the file.
file = open('patrons.txt', 'a')
# Once the file is open, write to it using the write method on the Variable file. The Write method accepts one Arg, so I have to use the + Operator to Concatenate text.
file.write (fname_info + ',')
file.write (mname_info + ',')
file.write (lname_info + ',')
file.write (suffix_info + ',')
file.write (addr1_info + ',')
file.write (addr2_info + ',')
file.write (city_info + ',')
file.write (state_info + ',')
file.write (zipcode_info + ';\n')
file.close ()
print (fname_info, 'has been registered')
# Clears the fields in the form
fname_entry.delete(0, END)
mname_entry.delete(0, END)
lname_entry.delete(0, END)
suffix_entry.delete(0, END)
addr1_entry.delete(0, END)
addr2_entry.delete(0, END)
city_entry.delete(0, END)
state_entry.delete(0, END)
zipcode_entry.delete(0, END)
# This code closes the window after entry
newWindow.destroy()
# End of saveInfo function
# Creating basic text that is formatted to be a header at the top of the window
lbl = Label(newWindow, text = 'Create Record', bg = 'light gray', fg = 'gray', width = '50', font = ('Arial', 17))
lbl.place(x=0, y=0)
# Defining Variables(1) for the text box labels:
fname_text = Label(newWindow, text = 'First Name', fg = 'gray', font = ('Arial', 12))
mname_text = Label(newWindow, text = 'Middle name', fg = 'gray', font = ('Arial', 12))
lname_text = Label(newWindow, text = 'Last Name', fg = 'gray', font = ('Arial', 12))
suffix_text = Label(newWindow, text = 'Suffix', fg = 'gray', font = ('Arial', 12))
addr1_text = Label(newWindow, text = 'Address 1', fg = 'gray', font = ('Arial', 12))
addr2_text = Label(newWindow, text = 'Address 2', fg = 'gray', font = ('Arial', 12))
city_text = Label(newWindow, text = 'City', fg = 'gray', font = ('Arial', 12))
state_text = Label(newWindow, text = 'State', fg = 'gray', font = ('Arial', 12))
zip_text = Label(newWindow, text = 'Zip', fg = 'gray', font = ('Arial', 12))
# Placing text box labels:
fname_text.place(x = 15, y = 50)
mname_text.place(x = 155, y = 50)
lname_text.place(x = 295, y = 50)
suffix_text.place(x = 435, y = 50)
addr1_text.place(x = 15, y = 100)
addr2_text.place(x = 15, y = 150)
city_text.place(x = 15, y = 200)
state_text.place(x = 318, y = 200)
zip_text.place(x = 367, y = 200)
# Defining and formatting Variables(2) for the data entered INTO the Entry boxes:
fname = StringVar()
mname = StringVar()
lname = StringVar()
suffix = StringVar()
addr1 = StringVar()
addr2 = StringVar()
city = StringVar()
state = StringVar()
zipcode = StringVar()
# Defining the Variables(3) for the Entry boxes:
fname_entry = Entry(newWindow, textvariable = fname, width = 14)
mname_entry = Entry(newWindow, textvariable = mname, width = 14)
lname_entry = Entry(newWindow, textvariable = lname, width = 14)
suffix_entry = Entry(newWindow, textvariable = suffix, width = 5)
addr1_entry = Entry(newWindow, textvariable = addr1, width = 51)
addr2_entry = Entry(newWindow, textvariable = addr2, width = 51)
city_entry = Entry(newWindow, textvariable = city, width = 32)
state_entry = Entry(newWindow, textvariable = state, width = 3)
zipcode_entry = Entry(newWindow, textvariable = zipcode, width = 13)
`enter code here`# Placing the Entry boxes:
fname_entry.place(x=15, y = 69)
mname_entry.place(x=155, y = 69)
lname_entry.place(x=295, y = 69)
suffix_entry.place(x=435, y = 69)
addr1_entry.place(x=15, y = 119)
addr2_entry.place(x=15, y = 169)
city_entry.place(x=15, y = 219)
state_entry.place(x=318, y = 219)
zipcode_entry.place(x=360, y = 219)
# Creates a Button and assigns it to the Variable: register. It is a gray, borderless button that when clicked, calls the saveInfo function.
register = Button(text = "REGISTER", bordercolor = 'gray', padx = 10, pady = 5, background='#BDBDBD', borderless = 'true', relief = 'groove', command = saveInfo)
# Places the Button
register.place (in_=newWindow, x = 200, y = 275)
newWindow.mainloop()
# End of createWindow function
style1 = ttk.Style()
style2 = ttk.Style()
style3 = ttk.Style()
style1.configure('topFrame.TFrame', background='green', borderwidth=4, relief='raised')
style2.configure('bottomFrame.TFrame', background = 'blue', borderwidth=6, relief = 'raised')
style3.configure('SrchLabel.TLabel')
eFrame = ttk.Frame(mainWindow, width=1060, height=200, style='topFrame.TFrame').grid()
rFrame = ttk.Frame(mainWindow, width=1060, height=600, style='bottomFrame.TFrame').grid()
# eFrame: Title text shown before the search terms entry box
srchFrame = ttk.Label(mainWindow, style = 'SrchLabel.TLabel', text = 'Patron Search')
srchFrame.place (x='30', y='40')
# eFrame: Search terms entry box
srchVar = StringVar()
srchEntry = tk.Entry(eFrame, bd = '1', width = '35')
srchEntry.place (x='130', y='37')
# eFrame: Button to execute Search
srchButton = Button(eFrame, text = 'Search', height = 30, width = 60, bd = '1')
srchButton.place(x = '470', y = '35')
# eFrame: Button to create New Record
newButton = Button(eFrame, text = 'New', height = 30, width = 60, bd = '1', command = createWindow)
newButton.place(x = '550', y = '35')
# rFrame: TAB Window Frame = from ttk Notebook
tabControl = ttk.Notebook(rFrame, width = 995, height = 528, style = 'rTabs.TNotebook')
# rFrame: TAB Elements
tab1 = ttk.Frame(tabControl)
tab2 = ttk.Frame(tabControl)
tab3 = ttk.Frame(tabControl)
tab1=Frame(tabControl, background="#ffffff")
tab1.pack()
tab2=Frame(tabControl, background="#ffffff")
tab2.pack()
tab3=Frame(tabControl, background="#ffffff")
tab3.pack()
# rFrame: TAB Titles and Window placement
tabControl.add(tab1, text ='Patron')
tabControl.add(tab2, text ='Orders')
tabControl.add(tab3, text = 'Donations')
tabControl.place(x='5', y='205')
# TAB 1 Content
ttk.Label(tab1, text ='Patron Data').grid(column = 0, row = 0, padx = 30, pady = 30)
# TAB 2 Content
ttk.Label(tab2, text ='Order History and Info').grid(column = 0, row = 0, padx = 30, pady = 30)
# TAB 3 Content
ttk.Label(tab3, text ='Donation Info').grid(column = 0, row = 0, padx = 30, pady = 30)
# mainWindow: MENUBAR
menubar = Menu(mainWindow)
# mainWindow: Filemenu
file = Menu(menubar, tearoff = 1)
menubar.add_cascade(label ='File', menu = file)
file.add_command(label ='New File', command = None)
file.add_command(label ='Open...', command = None)
file.add_command(label ='Save', command = None)
file.add_separator()
file.add_command(label ='Exit', command = mainWindow.destroy)
# mainWindow: Editmenu
edit = Menu(menubar, tearoff = 1)
menubar.add_cascade(label ='Edit', menu = edit)
edit.add_command(label ='Cut', command = None)
edit.add_command(label ='Copy', command = None)
edit.add_command(label ='Paste', command = None)
edit.add_command(label ='Select All', command = None)
edit.add_separator()
edit.add_command(label ='Find...', command = None)
edit.add_command(label ='Find again', command = None)
# mainWindow: Helpmenu
help_ = Menu(menubar, tearoff = 1)
menubar.add_cascade(label ='Help', menu = help_)
help_.add_command(label ='Tk Help', command = None)
help_.add_command(label ='Demo', command = None)
help_.add_separator()
help_.add_command(label ='About Tk', command = None)
mainWindow.config(menu = menubar)
mainWindow.mainloop()
Again, I am not looking for advice on how to write better code, as I am just learning (2 weeks in). However any help you can provide in letting me know what I am doing wrong and where I can find an answer would be greatly appreciated!
Thanks in advance!
I am new to Python, trying to make the Launch TFL App button open another GUI called "Menu GUI" but I don't know what to do for the def open_Menu(): function below. I want to use the popup GUI below as a launcher which takes the user to my main GUI. The only problem with the code below is that the button for launch TFL app doesn't do anything when you click it.
Here's my current code :
from tkinter import *
root = Tk()
root.title('TFL App')
p = Label(root, text = "TFL Journey Planner", height = "18", width = "250", bg = 'brown', fg =
'white',
font = ('Helvetica', '20', 'bold', 'italic'))
p.pack()
root.configure(bg = 'brown')
root.geometry('400x700')
photo = PhotoImage(file = 'trainstation.png')
label = Label(root, image = photo)
label.pack()
****#Buttons****
def open_Menu():
pass
Button1 = Button(root, text = "Launch TFL App", command = open_Menu, bg = "black", fg = 'white', padx
= 40,
pady = 10,
font = ('Calibri Light', '15', 'bold'))
Button1.pack(padx = 25, pady = 0)
Button2 = Button(root, text = "Exit ", command = root.destroy, bg = "black", fg = 'white', padx = 65,
pady = 8,
font = ('Calibri Light', '15', 'bold'))
Button2.pack(padx = 25, pady = 10)
root.mainloop()
How can I implement open_menu()
The code below is for my Main GUI which should open through the PopUp GUI above but the button on the PopUp GUI is not working.
from tkinter import *
def find():
# get method returns current text
# as a string from text entry box
From = From_field.get()
To = To_field.get()
travel_modes = mode_field.get()
# Calling result() Function
result(From, To, travel_modes)
# Function for inserting the train string
# in the mode_field text entry box
def train():
mode_field.insert(10, "train")
# Function for clearing the contents
def del_From():
From_field.delete(0, END)
distance_field.delete(0, END)
duration_field.delete(0, END)
def del_To():
To_field.delete(0, END)
distance_field.delete(0, END)
duration_field.delete(0, END)
def del_modes():
mode_field.delete(0, END)
distance_field.delete(0, END)
duration_field.delete(0, END)
def delete_all():
From_field.delete(0, END)
To_field.delete(0, END)
mode_field.delete(0, END)
distance_field.delete(0, END)
duration_field.delete(0, END)
# Driver code
if __name__ == "__main__":
# Create a GUI root
root = Tk()
# Set the background colour of GUI root
root.configure(background = 'light blue')
# Set the configuration of GUI root
root.geometry("600x400")
# Created a welcome to distance time calculator label
headlabel = Label(root, text = 'Welcome to your TFL Journey Planner',
fg = 'white', bg = "dark red", height = "0", width = "30",
font = ('calibri light', '19', 'italic'))
# Created a From: label
label1 = Label(root, text = "From:",
fg = 'white', bg = 'black')
# Created a To: label
label2 = Label(root, text = "To:",
fg = 'white', bg = 'black')
# Created a Distance: label
label4 = Label(root, text = "Distance:",
fg = 'white', bg = 'black')
# Created a Duration: label
label5 = Label(root, text = "Duration:",
fg = 'white', bg = 'black')
label6 = Label(root, text = "Choose travelling mode Below: ",
fg = 'white', bg = 'black')
headlabel.grid(row = 0, column = 1)
label1.grid(row = 1, column = 0, sticky = "E")
label2.grid(row = 2, column = 0, sticky = "E")
label4.grid(row = 7, column = 0, sticky = "E")
label5.grid(row = 8, column = 0, sticky = "E")
label6.grid(row = 3, column = 1)
# Created a text entry box
# for filling or typing the data.
From_field = Entry(root)
To_field = Entry(root)
mode_field = Entry(root)
distance_field = Entry(root)
duration_field = Entry(root)
From_field.grid(row = 1, column = 1, ipadx = "100")
To_field.grid(row = 2, column = 1, ipadx = "100")
mode_field.grid(row = 5, column = 1, ipadx = "50")
distance_field.grid(row = 7, column = 1, ipadx = "100")
duration_field.grid(row = 8, column = 1, ipadx = "100")
# CLEAR Button and attached
# to del_source function
button1 = Button(root, text = "Clear", bg = "light grey",
fg = "black", command = del_From)
# Create a CLEAR Button and attached to del_destination
button2 = Button(root, text = "Clear", bg = "light grey",
fg = "black", command = del_To)
# Create a RESULT Button and attached to find function
button3 = Button(root, text = "Result",
bg = "black", fg = "white",
command = find)
# Create a CLEAR ALL Button and attached to delete_all function
button4 = Button(root, text = "Clear All",
bg = "light grey", fg = "black",
command = delete_all)
# Create a Train Button and attached to train function
button5 = Button(root, text = "Train",
bg = "light grey", fg = "black",
command = train)
# Create a CLEAR Button and attached to del_modes function
button6 = Button(root, text = "Clear",
fg = "black", bg = "light grey",
command = del_modes)
button1.grid(row = 1, column = 2)
button2.grid(row = 2, column = 2)
button3.grid(row = 6, column = 1)
button4.grid(row = 9, column = 1)
button5.grid(row = 4, column = 1)
button6.grid(row = 5, column = 2)
root.mainloop()
Here is a solution using from subprocess import call. All you have to do is replace 'YOUR_FILE_NAME' with... your file name :D
from tkinter import *
from subprocess import call
root=Tk()
root.geometry('200x100')
frame = Frame(root)
frame.pack(pady=20,padx=20)
def open_Menu():
call(["python", "YOUR-FILE-NAME.py"])
btn=Button(frame,text='Open File',command=Open)
btn.pack()
root.mainloop()
What it will look like:
I hope this works for you :D
so im having issues with the function in my code and it doesnt display a fail in the console so i just dont know whats wrong.
basically im trying to make a clicker kinda thing where everytime the button is clicked it increases the exp variable by one and then updates the label, but it isnt really working. also the level part doesnt need much attention except if there is something you would like to say about it.
window = Tk()
window.title ("test")
#variables
exp2 = 0
level = 1
exp = 0
#exp function
def function ():
global exp,exp2, level
exp2 = exp + 1
if exp <100:
exp = 0
level + 1
else:
return function()
#labels
label_1 = Label (window, text = (exp2,"/100"), bg = 'white', fg ='black')
label_2 = Label (window, text = "exp" , bg = 'white', fg ='black')
label_3 = Label (window, text = "level", bg = 'white', fg ='black')
label_4 = Label (window, text = level, bg = 'white', fg ='black')
#buttons
button_1 = Button (window, text = 'click for xp', command = function())
#placements
label_1.grid (column = 3, row = 2)
label_2.grid (column = 3, row = 1)
label_3.grid (column = 5 , row = 1)
label_4.grid (column = 5, row = 2)
button_1.grid (column = 4, row = 4 )
window.mainloop()
def Function():
global exp, level
exp += 1
if exp > 100:
exp=0
level+=1
label.config(text=exp)
should work for you, you dont actually need the exp2 variable. The label.config is there for you
#python3
from tkinter import *
window = Tk()
window.title ("test")
#variables
#exp2 = 0
exp = 0
level=0
#exp function
def function ():
global exp,exp2, level
label_1.configure(text="%d" %(exp)+"/10")
exp+=1
if exp==10:
level+=1
label_4.configure(text=level)
exp=0
#labels
label_1 = Label (window,text="%d" %(exp)+"/10", bg = 'white', fg ='black')
label_2 = Label (window, text = "exp", bg = 'white', fg ='black')
label_3 = Label (window, text = "level", bg = 'white', fg ='black')
label_4 = Label (window, bg = 'white', fg ='black')
#buttons
button_1 = Button (window, text = 'click for xp', command = function)
#placements
label_1.grid (column = 3, row = 2)
label_2.grid (column = 3, row = 1)
label_3.grid (column = 5 , row = 1)
label_4.grid (column = 5, row = 2)
button_1.grid (column = 4, row = 4 )
window.mainloop()
Even though I don't really understand the question, I hope that it answered your question. You can stop the continuous count by removing the label_1.after(500,function). If this answer your question, vote
So i have a scenario when 'n' number of Independent radio groups have to be created and we shall be able to retreive value of each radio group independently.
Current code gives me the value of the last radio group irrespective of the button I press.
import tkinter # note that module name has changed from Tkinter in Python 2 to tkinter in Python 3
from tkinter import *
from tkinter import messagebox
top = tkinter.Tk()
top.title("NA")
a = int(input("Enter the number of frames"))
btn = [""]*a
def commd():
print(i)
print("Value changed to new value", values[i],'with value', values[i].get())
#print('Update in value')
values = [tkinter.IntVar() for i in range(len(btn))]
print(len(values))
for i in range(len(btn)):
print(i)
txt = "Button_Number", (i+1)
rtxt1 = 'Radio Button1',i+1
rtxt2 = 'Radio Button2',i+1
abc = Button(top, text = txt, activebackground = "Green", fg = "Blue", bg = "Red", height =5, command = aa)
R1 = Radiobutton(top, text = "START", activebackground = "Green", fg = "Blue", bg = "Red", height = 2,variable = values[i],value=0,command = commd)
R2 = Radiobutton(top, text = "STOP", activebackground = "Green", fg = "Blue", bg = "Red", height = 2,variable = values[i],value=1,command = commd)
abc.grid(row = i+i, column = 0,rowspan = 2)
R1.grid(row = i+i, column = 1)
R2.grid(row = i+i+1,column = 1)
mainloop()
Use .bind(... instead of command=... ...
Output:
Radio Button1.0 START
Radio Button2.1 STOP
Radio Button1.2 START
import tkinter as tk
class App(tk.Tk):
def __init__(self):
super().__init__()
self.title("NA")
for i in range(3):
print(i)
variable = tk.StringVar()
for n, text in enumerate(('START', 'STOP'), 1):
r = tk.Radiobutton(self, text=text, value=text, variable=variable)
r.bind('<ButtonRelease-1>', self.on_click)
r._group = f'Radio Button{n}.{i}'
r._variable = variable
r.grid(row=i, column=n)
def on_click(self, event):
_ = self
w = event.widget
print(w._group, w._variable.get())
if __name__ == '__main__':
App().mainloop()