'I have created a python application using pyinstaller on windows 10 64bit system. But it only opens gui and doesn't work on other windows 10 64 bit?
It was supposed to overlay one image over other and open that image but it's not working why can it be?? The real problem is that It works on some systems but not on others. https://drive.google.com/file/d/13DDMR1riSYgGqzh0PQscN6Zo-xlcJEPT/view?usp=sharing link of video showing how it opens the gui but doesn't work further. Code for GUI The GUI isn't good and the code may not be tidy but I am starting to code just now and I don't know much. It will be great if you could help debug it.
from tkinter import ttk
from PIL import Image, ImageTk
import overlay
import sqlite3
import sys
import os
from scrollableimg import ScrollableImage as scimg
data = sqlite3.connect('data.db')
c = data.cursor()
tables = c.execute("""
SELECT name FROM sqlite_master WHERE type = 'table';""").fetchall()
if len(tables) == 0:
c.execute("""CREATE TABLE position (x integer, y integer);""")
c.execute("""CREATE TABLE size (x integer, y integer);""")
data.commit()
root = Tk()
width= root.winfo_screenwidth()
height= root.winfo_screenheight()
#setting tkinter window size
root.geometry("%dx%d" % (width, height))
root.title('settings')
path = os.getcwd()
data.commit()
def click(event):
input_position.configure(state=NORMAL)
input_position.delete(0, END)
input_position.unbind('<Button-1>', positions)
def click1(event):
input_size.configure(state=NORMAL)
input_size.delete(0, END)
input_size.unbind('<Button-1>', sizes)
def change_position_size(path, position, size):
'''To change the position and size of qr code'''
overlay.remove(path)
if type(position) == tuple and type(size) == tuple:
overlay.overlay_img(path, position, size)
os.chdir(path)
for items in os.listdir():
if items.lower().endswith('.jpg') and items.lower().startswith('inv') and not items.lower().startswith('invoice') and not items.lower().startswith('invoicev'):
image = Image.open(path + '/' + items)
img1 = ImageTk.PhotoImage(image=image)
label = scimg(root, image = img1, scrollbarwidth = 9, width = width/1.0125, height = height/1.01)
label.grid(row = 4, column = 0, columnspan = 4)
#Delete present data from table
c.execute("DELETE FROM position")
c.execute("DELETE FROM size")
#insert new data
c.execute("INSERT INTO position (x, y) VALUES (?, ?)", (position[0], position[1]))
c.execute("INSERT INTO size (x, y) VALUES (?, ?)", (size[0], size[1]))
data.commit()
else:
label = Label(root, text = '!!!Enter position and size in 200, 198 format!!!', font = ('Arial', 13), foreground = 'red')
label.grid(row = 3, column = 0, columnspan = 2)
def test_input(position, size):
'''To check the correctness of input'''
position = position.split(', ')
size = size.split(', ')
new_position = []
new_size = []
if len(position) == 2 and len(size) == 2:
for x in position:
for y in x:
if not y in ('0123456789'):
print(sys.path)
raise ValueError('Enter numbers only')
new_position.append(int(x))
position = tuple(new_position)
for x in size:
for y in x:
if not y in ('0123456789'):
print(sys.path)
raise ValueError('Enter numbers only')
new_size.append(int(x))
size = tuple(new_size)
else:
print('position and size in 0, 0 format')
change_position_size(path, position, size)
#getting data from the database
position = c.execute("SELECT * FROM position").fetchall()
size = c.execute("SELECT * FROM size").fetchall()
input_position = Entry(root, width = 20, borderwidth = 5, font = ('Arial', 20))
input_position.grid( row = 1, column = 0, sticky = 'n')
if len(position) != 0:
input_position.insert(0, (str(position[0][0]) + ', ' + str(position[0][1])))
else:
input_position.insert(0, '0, 0')
positions = input_position.bind('<Button-1>', click)
text = Label(root, pady = 6, text = 'Please Enter The position For QR Code in 200, 198 format here and press enter ', font = ('Arial', 13))
text.grid(row = 0, column = 0)
input_size = Entry(root, width = 20, borderwidth = 5, font = ('Arial', 20))
input_size.grid(row= 1, column = 1)
if len(size) != 0:
input_size.insert(0, (str(size[0][0]) +', ' + str(size[0][1])))
else:
input_size.insert(0, '200, 200')
sizes = input_size.bind('<Button-1>', click1)
text1 = Label(root, pady = 6, text = 'Please Enter The Size For QR Code in 200, 198 format here and press enter ', font = ('Arial', 13))
text1.grid(row = 0, column = 1)
Enter = Button(root, text = 'Enter', height = 3, width = 10, command =lambda: test_input(input_position.get(), input_size.get()))
Enter.grid(row= 0, column = 2, pady = (12, 2), rowspan = 2)
root.bind('<Return>', lambda event=None: Enter.invoke())
root.mainloop()
data.close()
Code for Overlay which is imported in GUI used to overlay one image over other.
import os
import sqlite3
import string
def create_proper_img(path):
for items in os.listdir():
if 'invoice.jpg' == items.lower():
image = Image.open(path + '/' + items)
else:
if 'invoicev.jpg' == items.lower():
image3 = Image.open(path + '/' + items)
break
return image, image3
def resize_qr(path, size):
for items in os.listdir():
if items.lower().endswith('.gif') and items.lower().startswith('qr'):
with Image.open(path + '/' + items).convert('RGBA') as image1:
qr_image = image1.copy()
resized_qr = qr_image.resize((size), Image.Resampling.HAMMING)
break
return resized_qr
def overlay_img(path, position, size):
os.chdir(path)
for items in os.listdir():
if items.lower().endswith('.gif') and items.lower().startswith('qr'):
x = items.strip(string.ascii_letters + string.punctuation + string.whitespace)
break
list = os.listdir()
if 'INVOICE.jpg' in list and 'INVOICEV.jpg' in list and not "INV{x}.jpg".format(x = x) in list and type(position) == tuple and type(size) == tuple:
images = create_proper_img(path)
image = images[0]
image1 = images[1]
resized_qr = resize_qr(path, size)
copied_image = image.copy()
copied_image1 = image1.copy()
copied_image.paste(resized_qr, position, resized_qr)
copied_image1.paste(resized_qr, position, resized_qr)
copied_image1 = copied_image1.convert('RGB')
copied_image = copied_image.convert('RGB')
copied_image.save(path + '/INV' + x + '.jpg', dpi = image.info['dpi'])
copied_image1.save(path + '/INP' + x + '.jpg', dpi = image.info['dpi'])
def remove(path):
for item in os.listdir():
if not item.startswith('INVOICE') and not item.startswith('INVOICEV') and item.startswith('INV') and item.endswith('jpg'):
os.remove(item)
def main(path, position, size):
overlay_img(path, position, size)
if __name__ == "__main__":
path = os.getcwd()
os.chdir(path)
data = sqlite3.connect('data.db')
c = data.cursor()
position = c.execute("SELECT * FROM position").fetchall()
size = c.execute("SELECT * FROM size").fetchall()
main(path, position[0], size[0])
data.close()
for scrollbars
class ScrollableImage(tkinter.Frame):
def __init__(self, master=None, **kw):
self.image = kw.pop('image', None)
sw = kw.pop('scrollbarwidth', 10)
super(ScrollableImage, self).__init__(master=master, **kw)
self.cnvs = tkinter.Canvas(self, highlightthickness=0, **kw)
self.cnvs.create_image(0, 0, anchor='nw', image=self.image)
# Vertical and Horizontal scrollbars
self.v_scroll = tkinter.Scrollbar(self, orient='vertical', width=sw)
self.h_scroll = tkinter.Scrollbar(self, orient='horizontal', width=sw)
# Grid and configure weight.
self.cnvs.grid(row=0, column=0, sticky='nsew')
self.h_scroll.grid(row=1, column=0, sticky='ew')
self.v_scroll.grid(row=0, column=1, sticky='ns')
self.rowconfigure(0, weight=1)
self.columnconfigure(0, weight=1)
# Set the scrollbars to the canvas
self.cnvs.config(xscrollcommand=self.h_scroll.set,
yscrollcommand=self.v_scroll.set)
# Set canvas view to the scrollbars
self.v_scroll.config(command=self.cnvs.yview)
self.h_scroll.config(command=self.cnvs.xview)
# Assign the region to be scrolled
self.cnvs.config(scrollregion=self.cnvs.bbox('all'))
self.cnvs.bind_class(self.cnvs, "<MouseWheel>", self.mouse_scroll)
def mouse_scroll(self, evt):
shift = (evt.state & 0x1) != 0
scroll = -1 if evt.delta > 0 else 1
if shift:
self.cnvs.xview_scroll(scroll, "units")
else:
self.cnvs.yview_scroll(scroll, "units")```
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
