dropdown menu

Back To Home

Thursday, November 26, 2020

Solved: AttributeError_ type object 'Image' has no attribute 'open'

When we facing this issue, we need to add this module PIL.Image and use image=PIL.Image.open('/home/pi/Desktop/scene.jpg').


Code:

from tkinter import Tk, Canvas

from PIL import ImageTk, Image

from tkinter import *

import tkinter.font as tkFont

import PIL.Image   #Add this module

root = Tk()

topFrame =Frame(root,width=100,height=100)

topFrame.grid(row=1,column=0,rowspan=2)


def image1():

    print("hello")

#image=Image.open('/home/pi/Desktop/scene.jpg')    #comment out this

image=PIL.Image.open('/home/pi/Desktop/scene.jpg')   #use this 

photo = ImageTk.PhotoImage(image)

#a.config(image=mi,compound=BOTTOM)

a=Button(topFrame,image=photo,text="All stones preview",command=image1,height=290,width=335,font=tkFont.Font(weight="bold",size=15))

a.grid(row=1, column=1)

root.mainloop()


Video:



Sunday, September 6, 2020

Python tkinter: print preview

Print preview of text using python.


Python Code:

from tkinter import *

root = Tk()

configfile = Text(root, wrap=WORD, width=45, height= 20)

configfile.pack(fill="none", expand=TRUE)

filename='N.txt'

with open(filename,"rt", encoding="utf-8") as f:

    configfile.insert(INSERT, f.read())





Monday, June 29, 2020

Python - Read open excel file data

Sometimes we want to read real time update data for excel that haven't save.
So below code is useful.


Python code:

import win32com.client as win32
import pandas as pd

#here we use win32com to save open excel
excel = win32.gencache.EnsureDispatch('Excel.Application')
print("Active WB:", excel.ActiveWorkbook.Name)
for wb in excel.Workbooks:
    if wb.Name == 'da.xlsx' :

        print("WB:",wb.Name)
        wb.Save()

#here we use panda module to read and check
df = pd.read_excel (r'da.xlsx', sheet_name='Sheet1')
print (df)


Monday, May 25, 2020

Solved: UnicodeEncodeError: 'charmap' codec can't encode characters

Below are code to solve this error.

Code:


import xlsxwriter 
  
workbook = xlsxwriter.Workbook('skill.csv') 
  
# By default worksheet names in the spreadsheet will be  
# Sheet1, Sheet2 etc., but we can also specify a name. 
worksheet = workbook.add_worksheet("skill") 
  
# Some data we want to write to the worksheet. 
details = ( 
    ['SN', 'Name','Contribution'], 
    [1, "Linus Torvalds", "Linux Kernel"], 
    [2, "Tim Berners-Lee", "World Wide Web"], 
    [3, "Guido van Rossum", "Python Programming"],
    [4, "吴刚", "中文"],
 ) 

# Start from the first cell. Rows and 
# columns are zero indexed. 
row = 0
col = 0
  
# Iterate over the data and write it out row by row. 
for SN, Name , Contribution in (details): 
    worksheet.write(row, col, SN) 
    worksheet.write(row, col + 1, Name)
    worksheet.write(row, col + 2, Contribution)
    row += 1
  
workbook.close() 




Saturday, May 23, 2020

python - table generation in pdf using python

First need to install module reportlab. Then create table using code below:

Output pdf:



Code:

from reportlab.lib import colors   #import module
from reportlab.lib.pagesizes import letter, inch
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle

doc = SimpleDocTemplate("simple_table_grid.pdf", pagesize=letter)   #define pdf document name
# container for the 'Flowable' objects
elements = []   #define the data

data= [['00', '01', '02', '03', '04'],
       ['10', '11', '12', '13', '14'],
       ['20', '21', '22', '23', '24'],
       ['30', '31', '32', '33', '34']]
t=Table(data,5*[0.8*inch], 4*[0.8*inch])     #define the table format width and legth
t.setStyle(TableStyle([('ALIGN',(1,1),(-2,-2),'RIGHT'),
                       ('TEXTCOLOR',(1,1),(-2,-2),colors.red),
                       ('VALIGN',(0,0),(0,-1),'TOP'),
                       ('TEXTCOLOR',(0,0),(0,-1),colors.blue),
                       ('ALIGN',(0,-1),(-1,-1),'CENTER'),
                       ('VALIGN',(0,-1),(-1,-1),'MIDDLE'),
                       ('TEXTCOLOR',(0,-1),(-1,-1),colors.green),
                       ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
                       ('BOX', (0,0), (-1,-1), 0.25, colors.black),
                       ]))

elements.append(t)        #then write the data
# write the document to disk

doc.build(elements)




Tuesday, May 5, 2020

Python tkinter - textbox


Here is the code for python gui textbox.


Python code:

from tkinter import *

root=Tk()

name=StringVar()

a=Entry(root,textvariable=name)
a.pack()

def enter():
    print(name.get())

    print("the text we get"+name.get())


b=Button(root,text="enter",command=enter)
b.pack()

root.mainloop()

Saturday, April 11, 2020

No module named PIL solved

No module named PIL solved.
At command propmpt: pip install pillow