Merging PyOpenGL with Tkinter - python-3.x

PyOpen GL with Tkinter
Hey guys how can I implement OpenGL into tkinter?
Should i use Pygame? But when I do so - only glClear(...) works.
I've heard about OpenGL.Tk but the problem is that when i want to import it, python gives me the following exception:
And here is my code I've written
import csv
from tkinter import *
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.Tk import *
import os
from numpy import *
root = Tk()
canvas = Frame(root,width=500,height=500).pack()
os.environ['SDL_WINDOWID'] = str(canvas.winfo_id())
os.environ['SDL_VIDEODRIVER'] = 'windib'
pygame.init()
pygame.display.set_mode((500,500), OPENGL|DOUBLEBUF)
gluOrtho2D(-10,10,-10,10)
while True:
glClearColor(0.2,0.2,0.2,1)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glBegin(GL_POINTS)
glColor3f(1,1,1)
glVertex2f(0,0)
glEnd()
root.update()
pygame.display.flip()
pygame.time.wait(30)
Have you got any idea how can I make this working?
I've spent most of the time today to search for it...

Related

Is there a way to use the ffmpeg binary/unix executable in a py2app application to run ffmpeg on computers without it installed?

I wrote a small python script that is essentially just a text to speech script. It uses the pydub - audiosegment python library to convert the mp3 from gTTS to an ogg that can be played in pygame. A link to my github repository can be found here: https://github.com/AnupPlays/TTS
this is the main function:
def webscrape():
global x
global b
b.state(['disabled'])
src = "sound.mp3"
dst = "sound.ogg"
murl = str(url.get())
response = requests.get(murl)
response.raise_for_status()
parse = bs4.BeautifulSoup(response.text, 'html.parser')
x = str(parse.get_text())
print(x)
text = gTTS(x)
text.save("sound.mp3")
AudioSegment.from_mp3(src).export(dst, format='ogg')
b.state(['!disabled'])
this is a list of my imports:
#Imports
import os
import sys
import pygame
#google text to speech
from gtts import gTTS
#requests and BeautifulSoup
import requests
import bs4
#pygame audio player
from pygame import mixer
#tkinter ui
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from tkinter import messagebox
#mp3 -> wav
from os import path
from pydub import AudioSegment
For anyone wondering using homebrew you can get the dependencies for this and copy those dependencies into your packager.

Problems with pyinstaller doesn´t show all things that I created

I've been working on an app to manage a little store with Python and SQLite. For GUI I used tkinter. I've written 13 Python scripts to make the app work. One of them call "principal.py" which imports most of the scripts and the other scripts import other modules like tkcalendar or xlwt (to write excel sheets), etc...
This is an example from "principal.py" first lines of code
#===========IMPORTAR PAQUETES=============
import sqlite3
from sqlite3 import Error
import os
import sys
import tkinter as tk
from tkinter import ttk
from tkinter import *
from tkinter import scrolledtext
from tkinter import Menu
from tkinter import messagebox as msg
import tkcalendar
from tkcalendar import *
import conexion as cnx
import proveedores
from proveedores import Proveedores
from proveedores import *
import productos
from productos import Categoria_Productos
from productos import *
import clientes
from clientes import Clientes
from clientes import *
import colaboradores
from colaboradores import Colaboradores
from colaboradores import *
import herramientas
from herramientas import Roles
from herramientas import Usuarios
from herramientas import *
import recepciondoc
from recepciondoc import Recepcion
from recepciondoc import *
import ventas
from ventas import Ventas
from ventas import *
import ingresos
from ingresos import *
import movimientos
from movimientos import *
import excel_ingresos
from excel_ingresos import *
import excel_venta
from excel_venta import *
This is how my files are structured and how it works when I run the script from vscode
But when I tried to make an exe from my little project using PyInstaller, it looks incomplete like this:
this is how it looks like when I run the exe file from the "dist" folder
I worked like this
Create the spec file with pyi-makespec principal.py
Re-write the principal.spec to save the database path
pyinstaller --windowed principal.spec

Python 3 neopixel non-functional

import neopixel
import board
import sounddevice as sd
from numpy import linalg as LA
import numpy as np
def getvolume(indata, outdata, frames, time, status):
volume_norm = np.linalg.norm(indata)*10
with sd.Stream(callback=getvolume):
sd.sleep(250)
pixels = neopixel.NeoPixel(board.D18, 144)
pixels.fill((volume_norm, volume_norm, volume_norm))`
I want the code to make the LED strip shine brighter the louder the sound is. It currently doesn't do anything. What am I doing wrong?

Live Screen Monitoring With Python3 Pytesseract

I am working on a python3 project on windows 10, and I was wondering if anyone knew of anyway to pass an opencv screen grab through pytesseract? If not, is there any other OCR that you could?
Here is the code for the opencv screen grab:
import numpy as np
from PIL import ImageGrab
import cv2
while True:
screen = np.array(ImageGrab.grab(bbox=(0,40,800,640)))
cv2.imshow('window', cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY))
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
I know very little about pytesseract, but this might get you started:
#!/usr/bin/env python3
import numpy as np
from PIL import ImageGrab
import pytesseract
from PIL import Image, ImageEnhance, ImageFilter
from textblob import TextBlob
# Grab some screen
screen = ImageGrab.grab(bbox=(0,0,800,640))
# Make greyscale
w = screen.convert('L')
# Save so we can see what we grabbed
w.save('grabbed.png')
text = pytesseract.image_to_string(w)
correctedText = TextBlob(text).correct()
print(correctedText)
From this grab:
I got:
# Terminal Shell Edit View Window Help
The writing is on the wall

Python 3.7, tkinter, jpg: couldn't recognize data in image file

I wanted to ask some help regarding tkinter, in python3.
I can't seem to display a jpeg image file in a label using the following code:
def changephoto(self):
self.tmpimgpath = filedialog.askopenfilename(initialdir=os.getcwd())
self.installimagepath.set(self.tmpimgpath)
self.selectedpicture = PhotoImage(file=self.installimagepath.get())
self.PictureLabel.configure(image=self.selectedpicture)
It can do png Images just fine, but when I try to load a jpg image, all I i am able to get is the following error:
_tkinter.TclError: couldn't recognize data in image file
I went through all similar questions I could find, but they all seem to answer the same thing: "from PIL import ImageTk, Image"
When I try that ( for the moment, I am trying to use pillow, btw ), ImageTk doesn't seem to be available.
Any help would be greatly appreciated.
You have to install PIL: pip install pillow.
If pip does not successfully install pillow, you might have to try pip3 or pip3.7 (use bash to see which options you have)
You can open your image with ImageTk:
import os
import tkinter as tk
from tkinter import filedialog
from PIL import ImageTk
def changephoto():
root = tk.Tk()
PictureLabel= tk.Label(root)
PictureLabel.pack()
tmpimgpath = filedialog.askopenfilename(initialdir=os.getcwd())
selectedpicture= ImageTk.PhotoImage(file=tmpimgpath)
PictureLabel.configure(image=selectedpicture)
Solution provided by Chuck G worked. I can't tell why I initially couldn't import ImageTk, but that ended up to just work.
from PIL import ImageTk
This error could possibly happen because of relative file path or non-English characters in filepath ,So i made this function which works very good in windows and with any kinds of file paths :
def loadrelimages(relativepath):
from PIL import ImageTk, Image
import os
directory_path = os.path.dirname(__file__)
file_path = os.path.join(directory_path, relativepath)
img = ImageTk.PhotoImage(Image.open(file_path.replace('\\',"/")))
return img
for example load photo_2021-08-16_18-44-28.jpg which is at the same directory with this code:
from tkinter import *
import os
def loadrelimages(relativepath):
from PIL import ImageTk, Image
import os
directory_path = os.path.dirname(__file__)
file_path = os.path.join(directory_path, relativepath)
img = ImageTk.PhotoImage(Image.open(file_path.replace('\\',"/")))
return img
root = Tk()
canvas = Canvas(root, width=500, height=500)
canvas.pack()
loadedimage=loadrelimages('photo_2021-08-16_18-44-28.jpg')
canvas.create_image(250, 250, image=loadedimage)
root.mainloop()
try this!!!!

Resources