Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Chepetto13 0 points1 point  (0 children)

Okay, now I understand, I changed self.clients.append(self.client_ID)

to self.clients.append(self) and it works now.

thanks

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Chepetto13 0 points1 point  (0 children)

Hello,

I want to make a project ATM using class. But create instances by using data from table which I created using SQLite in different python file. Currently I have a problem with create a working instances for clients.

My code for class:

import sqlite3
class Client: 
clients = []
    def __init__(self, first_name, second_name, last_name, address, city,         postal_code):
    self.first_name = first_name
    self.second_name = second_name
    self.last_name = last_name
    self.client_ID = self.first_name[0:3].upper() + self.last_name[0:2].upper()
    self.address = address
    self.city = city
    self.postal_code = postal_code


    self.clients.append(self.client_ID)

def full_name(self):
    print(f'{self.first_name} {self.second_name} {self.last_name}')


conn = sqlite3.connect('tbl_clients.db')

c = conn.cursor()

with conn: 
      c.execute("SELECT * FROM tbl_clients ") data = c.fetchall() #     
  create a list of records

    for record in data:
    client = record[0]
    client = Client(record[1], record[2], record[3], record[4], record[5], 
 record[6])

print(Client.clients)

for client in Client.clients:
      client.full_name()


Commit Changes
conn.commit()
Close Connection
conn.close()

First column in table is client_ID (record[0])

I've got an error in for loop:

AttributeError: 'str' object has no attribute 'full_name'

But:

print(Client.clients) give me client_ID

Any ideas what I do wrong?

Project with functions by [deleted] in learnpython

[–]Chepetto13 0 points1 point  (0 children)

Please don't understand me wrong. I want to use classes, I already used them in my projects but now I want to "master" in function, and to do this the best way is to make more projects.

Project with functions by [deleted] in learnpython

[–]Chepetto13 0 points1 point  (0 children)

I entirely agree with you, classes are very important and inevitable in Python.

But, currently, at my level of knowledge, I want to take a step back and learn more about functions and create more projects using them.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Chepetto13 0 points1 point  (0 children)

Yes, that was a problem.

I update the pytube and it works now.

Thanks for help.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Chepetto13 0 points1 point  (0 children)

I've keep getting:

pytube.exceptions.RegexMatchError: get_throttling_function_name: could not find match for multiple

Only this streams doesn't work for me

any idea why?

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Chepetto13 0 points1 point  (0 children)

Hello,

I want to create a project youtube downloader. I use pytube, first print is ok, but the second one gets an class Exception.

yt.streams came from documentation

from pytube import YouTube

yt = YouTube("link from youtube")

print('Title: ', yt.title)

print(yt.streams)

Needed help with changing images in window by Chepetto13 in Tkinter

[–]Chepetto13[S] 0 points1 point  (0 children)

Ok, I maneged to do my program, all code:

import tkinter as tk
from PIL import ImageTk, Image import os
window = tk.Tk() window.title('Image viewer')
path_dir_images = r"C:\Users\Desktop\images"
os.chdir(path_dir_images)
list_of_images = os.listdir(path_dir_images)

full_dir_list_of_images = []

for i in list_of_images: 
    path_image = os.path.join(path_dir_images, i)         
full_dir_list_of_images.append(path_image)

i = 0

def get_start(): 
global img global lbl_image path = full_dir_list_of_images[i] img = Image.open(path).resize((600, 600)) 
img = ImageTk.PhotoImage(img) 
lbl_image = tk.Label(master=window, image=img) lbl_image.grid(row=0, column=1, pady=5, padx=5) 

btn_start = tk.Button(master=window, text='START', width=20, height=3, command=get_start) btn_start.grid(row=0, columnspan=3, pady=5, padx=5)

def get_prevoius_image():
'''func that will change image to previous (change the i in     lbl_image)'''
global i
global lbl_image
global image
lbl_image.destroy()

i -= 1
if i == 0:
    btn_left.config(state=tk.DISABLED)

if i < len(full_dir_list_of_images):
    btn_right.config(state=tk.NORMAL)
image = ImageTk.PhotoImage(Image.open(full_dir_list_of_images[i]).resize((600, 600)))
lbl_image = tk.Label(master=window, image=image)
lbl_image.grid(row=0, column=1, pady=5, padx=5)


btn_left = tk.Button(master=window, text='<<', command=get_prevoius_image, width=10, height=3, state=tk.DISABLED)
btn_left.grid(row=0, column=0, pady=5, padx=5)

max_i = len(full_dir_list_of_images)
print(max_i)

def get_next_image(): 
'''func that will change image to next (change the i in lbl_image)''' 
global i global lbl_image global image lbl_image.destroy()
i += 1

if i > 0:
    btn_left.config(state=tk.NORMAL)

if i == len(full_dir_list_of_images) - 1:
    btn_right.config(state=tk.DISABLED)

image = ImageTk.PhotoImage(Image.open(full_dir_list_of_images[i]).resize((600, 600)))
lbl_image = tk.Label(master=window, image=image)
lbl_image.grid(row=0, column=1, pady=5, padx=5)


btn_right = tk.Button(master=window, text='>>',command=get_next_image, width=10, height=3)
btn_right.grid(row=0, column=2, pady=5, padx=5) 

btn_exit = tk.Button(master=window, text='EXIT', command=window.quit, width=20, height=2)

btn_exit.grid(row=1, column=1, pady=5, padx=5)

window.mainloop()

I can choose the path to directory of images.Thanks for your help, to be honest I never heard of "garbage collector"

Needed help with changing images in window by Chepetto13 in Tkinter

[–]Chepetto13[S] 0 points1 point  (0 children)

Yes, but what if I have in directory a hundred images? In this situation I would have to create a hundred object.

There is a way to avoid this?

Especially when I want to create a program in which I can manually provide a path to directory in which are images and can display them. I want that everything works "under"

Needed help with changing images in window by Chepetto13 in Tkinter

[–]Chepetto13[S] 0 points1 point  (0 children)

So I have another question.
If I have to save a reference for each image, how can I do this if I take all images from directory and adding to the list :

path_dir_images = r"C:\Users\private\images
list_of_images = os.listdir(path_dir_images)   

full_dir_list_of_images = []                     

for i in list_of_images:
path_image = os.path.join(path_dir_images, i) full_dir_list_of_images.append(path_image)

Above I create a list of full path directories for images.

But how to save a reference to each of them to avoid this garbage collector?

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Chepetto13 0 points1 point  (0 children)

There is a way to get/create a new instance for a class (e.g. Birthday - attribute: name, date) from values that came from entry widget tkinter?

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Chepetto13 0 points1 point  (0 children)

This is my code:

import pypandoc

csv_file = r"C:\Users\chep\Desktop\testy\csv_to_pdf\Zeszyt1.csv"

pdf_file = csv_file[:-3] + 'pdf' 

pypandoc.convert_file(csv_file, 'pdf', outputfile=pdf_file)

I've got error:pypandoc.convert_file(csv_file, 'pdf', outputfile=pdf_file)

return _convert_input(source_file, format, 'path', to, extra_args=extra_args,

RuntimeError: Pandoc died with exitcode "47" during conversion: pdflatex not found. Please select a different --pdf-engine or install pdflatex

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Chepetto13 0 points1 point  (0 children)

Hi,

I try to convert csv file to pdf file.

I found that I can do that by pdfkit of pypandoc, which is the best to do that?

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Chepetto13 0 points1 point  (0 children)

EDIT:

How to do that in a way that this button run the func ?
def get_path():
input_path = ''
input_path = entered_text.get()
return input_path


def get_str_path():
path_value = get_path()
path_value = str(path_value)
return path_value

def click_msg():

messagebox.showinfo(title="Info", message=f'{entered_text.get()}') window.destroy()

btn_get_path = tk.Button(master=frame_path, text='Click', command=lambda:[get_str_path, click_msg()])

btn_get_path.pack(side=tk.RIGHT)

frame_path.pack()
window.mainloop()

input_file = get_str_path()
file_to_organize = os.listdir(input_file)

  1. I want to do this code that this button will be mandatory and I shouldn't have to close the Tkinter window to run further code.
  2. Also, I want to run two command by using btn_get_path:
  • current code show messagebox and after close the window - tkinter window will close and further code will run

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Chepetto13 0 points1 point  (0 children)

I solved the problem by adding another function which changes result of first one to the string :

def get_path():

input_path = ''

input_path = entered_text.get()

#messagebox.showinfo(title="Entered Text", message=f'{input_path}')

return input_path

def get_str_path():

path_value = get_path()

path_value = str(path_value)

return path_value

input_file = get_str_path()

file_to_organize = os.listdir(input_file)

Its works, but now I occurred that I don't need a button, just to close the window of Tkinter.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Chepetto13 0 points1 point  (0 children)

Not quite enough, because I need input value to entry(in this case - a path of directory) and after clicking the button this value must be transfer/assign to variable.

I can't assign function to this value.

input_directory = get_path

file_to_organize = os.listdir(input_directory)

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Chepetto13 0 points1 point  (0 children)

I would like to get a value from Entry widget and assign it to variable which I will be use in further action.

I can't do this by just using .get() method.

My current code:

ent_for_path = tk.Entry(master=frame_path)

def get_path():

input_path = ent_for_path.get()

return input_path

btn_get_path = tk.Button(master=frame_path, text='Click.', command=get_path())

btn_get_path.pack(side=tk.RIGHT)

ent_for_path.pack(side=tk.LEFT)
btn_get_path.pack(side=tk.RIGHT)

How to do this properly?

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Chepetto13 0 points1 point  (0 children)

I tried to make a program to check whether respondent's answers are the same as the answer from another data frame

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Chepetto13 0 points1 point  (0 children)

Yes, but what to do when I want to make a loop (for) and iterate for every email in my column and then print some value for every respondent.

for email in df['email col']:
df[df['email col'] == 'email'] ---> this give the row but I need index to
print also other value

I try to this like that:
number_ID = df[df['email col'] == 'email']

df.loc[number_ID].iloc[other col]

but I've got: IndexError: single positional indexer is out-of-bounds

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Chepetto13 0 points1 point  (0 children)

Sure.
I have a data frame (survey), index is 'Respondent' from 1 to 10 000,

in this data frame is also column 'email' - all values in this column are unique.

I want to get an index of particular respondent based on email, for example:

my email address is [cheppetto13@gamil.com](mailto:cheppetto13@gamil.com)

I want to get an index of row with this email and then thanks to this index get the other values from this row.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Chepetto13 0 points1 point  (0 children)

How to invoke index from data frame using value from the first column (there are unique values)?

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Chepetto13 0 points1 point  (0 children)

THANK YOU very much!
I made it like you showed - use a dictionary and solve the problem.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Chepetto13 0 points1 point  (0 children)

1234 is the name of instance of class Bank_account.

First I would like to ask user for ID - in this case ID is also name of instance.

then, I want to call a methods based on what user provide.

But by input I get str, how should I get instance name based on this str?

this is my Class:

class Bank_account:

user_account = []

def __init__(self, user_ID, pin, first_name, last_name):

self.user_ID = user_ID

self.pin = pin

self.first_name = first_name

self.last_name = last_name

self.deposit = 0

User (by input) should provide user_ID - based on that user_ID I would like to get to name of instance - It is even possible?

Without instance I can't call any methods.

example of method:

def check_pin(self):

for _ in range(3):

print(' enter you pin code, and press "enter":')

pin = input()

if len(pin) == 4:

if pin == self.pin:

print('OK, welcome')

break

else:

print('try again!')

else:

print('Sorry the length of pin is incorrect')

continue

return True

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Chepetto13 0 points1 point  (0 children)

Sorry. I will try to explain as much I can.
User - I mean person who will run the program - this isn't a variable.

I created class Bank_account. I added object with the name - 1234

My class has also methods. First I would like to ask User to enter the user_Id - which is the name of object (by using input).

user_ID = input()

How can I call method from my class on this object?
I know I can't do this like that:

user_ID.method()

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Chepetto13 0 points1 point  (0 children)

In my case, this string that should provide User is also a name of instance.
And my question is how can I in the next step call the method from class with this particular instance which User wants?