Python Forum
class random var write to array
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
class random var write to array
#1
Hi guys!
I got problem with my code. I wish in everytime new numbers from "Randomi" give it to class "data". After that write in row in array "arr" in order x,y,r,g,b.
So problem is with defind tha x,y,r,b,g (i know i defind in class and give numbers in randomi but in main nothing happ Dodgy ). Ty for your help!

import random
import numpy as np

arr = np.empty((1, 5), dtype=object)

class data:
  def __init__(self, x,y,r,g,b):
    self.x=rx
    self.y=ry
    self.r=rr
    self.g=rg
    self.b=rb
def Randomi():
  for i in range(5):
    rrr=random.randint(0,100)
    rx=ry=rr=rg=rb=rrr

def main():
  Randomi()
  p=data(x,y,r,g,b)
  for character in 'xyRGB':
    print(character)
    for i in range (0,5):
      arr [0,i]=p.character
  print(arr)

if __name__ == "__main__":
    main()
Reply
#2
I understand nothing.
Reply
#3
Your code is complete garbage.
Did you ever work on a Python tutorial for more than 5sec?
I strongly advise you to look here: 10 free Python Programming Courses
Reply
#4
(Aug-02-2019, 11:58 AM)ThomasL Wrote: Your code is complete garbage.
Did you ever work on a Python tutorial for more than 5sec?
I strongly advise you to look here: 10 free Python Programming Courses

Sir ty for rate my work. Trust me i step tutorial not for one or two Times. This is part of bigger project. This "garbage" is was wrote for understood mehanic.
To understood:
IS part of Mouse record. Randomi was submitted for varible another program who give 5 varible x, y Mouse position and r, g, B pixel color. Hole this program is to Save that var in array. I was make it step by step via tutorials(working with class, metod).
Reply
#5
I tried to refactor your code in a way it makes sense from what i assume to understand you want to do.
Look and learn.
import numpy as np
 
class Data:
    
    def __init__(self, x, y, r, g, b):
        self.x = x
        self.y = y
        self.r = r
        self.g = g
        self.b = b
        # or to make in some way possible what you want to do
        self.data = {'x':x, 'y':y, 'R':r, 'G':g, 'B':b}
    
def randomi():
    return [np.random.randint(0, 100) for i in range(5)]
    

def main():
    arr = np.zeros((5), dtype='int64')
    print(arr)
    
    x, y, r, g, b = randomi()
    print(x, y, r, g, b)
    
    p = Data(x, y, r, g, b)
    
    for i, character in enumerate('xyRGB'):
        arr[i] = p.data[character]
        print(character, arr[i])
    
    print(arr)

    
if __name__ == "__main__":
    main()
Reply
#6
(Aug-02-2019, 02:21 PM)ThomasL Wrote: I tried to refactor your code in a way it makes sense from what i assume to understand you want to do.
Look and learn.
...
def randomi():
return [np.random.randint(0, 100) for i in range(5)]
...
x, y, r, g, b = randomi()
...
for i, character in enumerate('xyRGB'):
arr[i] = p.data[character]
print(character, arr[i])

Ty is hard, for me translate my algorythm to code, so in fir i try in simply way thenafter that i try to cut and short code.
In randomi i dont know that we can give every var def randomi:D that is good ty for your help
in second "for i, character in enumerate('xyRGB')" now i understood my mistake abouth characters.

I am very grateful! Heart
no i can transport all data from another program in def randomi and swap

here is code about that mouse position i use pyautogui. This code is not done is some errors with "self" i try make window where is button to start another window who follow mouse and give coords. That def is work perfect if we do stand alone from first window...

import tkinter as tk
import pyautogui, time

class MainWindow(tk.Frame):
    counter = 0
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        self.button = tk.Button(self, text="Create new window",
                                command=self.create_window)
        self.button.pack(side="top")



    def create_window(self):
        self.counter += 1
        t = tk.Toplevel(self)
        t.wm_title("Window #%s" % self.counter)

        x, y = pyautogui.position()
        pixelColor = pyautogui.screenshot().getpixel((x, y))
        x = x + 2
        y = y + 1
        loc = "150x45+" + str(x) + "+" + str(y)
        root.geometry(loc)
        root.attributes("-topmost", True, "-toolwindow", True, "-alpha", 0.7)
        root.after(25, move)
        pole = Label(root, text="X:" + str(x) + " Y:" + str(y) + "\n R:" + str(pixelColor[0]).rjust(3) + " G:" + str(
            pixelColor[1]).rjust(3) + " B:" + str(pixelColor[2]).rjust(3))
        pole.place(x=0, y=0) 



if __name__ == "__main__":
    root = tk.Tk()
    main = MainWindow(root)
    main.pack(side="top", fill="both", expand=True)
    root.mainloop()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How Write Part of a Binary Array? Assembler 1 305 Jan-14-2024, 11:35 PM
Last Post: Gribouillis
  How do I write class objects to a file in binary mode? Exsul1 7 5,649 Sep-14-2019, 09:33 PM
Last Post: snippsat
  Multi-Dimm Array or Class? PappaBear 2 2,224 May-02-2019, 07:04 PM
Last Post: PappaBear
  20 x 20 Integer array with random numbers harryk 3 3,288 Jul-28-2018, 03:38 PM
Last Post: harryk
  Write selected (random) columns to output-file anjohepa 0 2,312 Feb-27-2018, 02:19 PM
Last Post: anjohepa
  Best construct? Array, class, other? PappaBear 1 2,946 May-10-2017, 06:02 PM
Last Post: nilamo
  What work faster and take less memory array or class? Kamilbek 1 3,097 Apr-20-2017, 05:32 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020