![]() |
class random var write to array - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: class random var write to array (/thread-20246.html) |
class random var write to array - storzo - Aug-02-2019 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 ![]() 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() RE: class random var write to array - fishhook - Aug-02-2019 I understand nothing. RE: class random var write to array - ThomasL - Aug-02-2019 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 RE: class random var write to array - storzo - Aug-02-2019 (Aug-02-2019, 11:58 AM)ThomasL Wrote: Your code is complete garbage. 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). RE: class random var write to array - ThomasL - Aug-02-2019 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() RE: class random var write to array - storzo - Aug-02-2019 (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. 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! ![]() 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() |