Create list of tkinter canvas lines/points - python-3.x

Many lines and points possible that I would like to be able to track when I line/point has been moused over. Is there any short codeable way of doing it or do I half to come up with hundreds/thousands of different element names.
I've tried
self.z[0].canvas.create_line()
self.z[1].canvas.create_line()
as well as
self.z(0).canvas.create_line()
self.z(1).canvas.create_line()
to only get back an error saying something like z can't be an integer, aka you can't do that stupid:)
Is there anyway to set up a nice for loop and create the lines/points and then be able to test test them once they are created. I can test the points the way I want to be able to test them but I would just like an easier way of creating the lines/points.
Worst case scenario is there a way of setting up something like
self.z1.canvas
self.z2.canvas
self.z3.canvas
but have 1,2,3 each be able to be increased through a for loop? I'm not sure if I have ever seen something like what I'm suggesting be made mention of or not.

Every time you create an item on a canvas, it returns a unique id. You can store that id in a list.
self.lines = []
for x in range(1000):
item = self.canvas.create_line(...)
self.lines.append(item)
That being said, you don't need to keep any of these in an array to " track when I line/point has been moused over.". You can set up bindings for that.

Related

How to initilise a list that contains custom functions without python running those functions during initialisation?

Short version:
How do you store functions in a list and only have them be executed when they are called using their index position in the list?
Long Version:
So I am writing a program that rolls a user-chosen number of six-sided dice, stores the results in a list and then organizes the results/ data in a dictionary.
After the data is gathered the program gives the user options from 0-2 to choose from and asks the user to type a number corresponding to the option they want.
After this input by the user, a variable, lets say TT, is assigned to it. I want the program to use TT to identify which function to run that is contained within a list called "Executable_options" by using TT as the index posistion of this function within the list.
The problem I am having is that I have to have the list that contains the functions on a line after the functions have been defined and when I initialize the list it goes through and executes all functions within it in order when I don't want it to. I just want them to be in the list for calling at a later date.
I tried to initialise the list without any functions in and then append the functions individually, but every time a function is appened to the list it is also executed.
def results():
def Rolling_thunder():
def roll_again():
The functions contains things, but is unnecessary to show for the question at hand
Executable_options = []
Executable_options.append(results())
Executable_options.append(Rolling_thunder())
Executable_options.append(roll_again)
options = len(Executable_options)
I am relatively new to Python so I am still getting my head around it. I have tried searching for the answer to this on existing posts, but couldn't find anything so I assume I am just using the wrong key words in my search.
Thank you very much for taking the time to read this and for the answers provided.
Edit: Code now works
The () on the end of the function name calls it - i.e. results() is the call to the results method.
Simply append to the list without the call - i.e:
Executable_options.append(results)
You can then call it by doing e.g.:
Executable_options[0]()
as per your given data the code will look like this:
def results():
def Rolling_thunder():
def roll_again():
Executable_options = []
Executable_options.append(results)
Executable_options.append(Rolling_thunder)
Executable_options.append(roll_again)
for i in range(0,len(Executable_options)):
Executable_options[i]()
this will work for you.

How to print specific line from the GET request Output

Hi Stackoverflow community, I am just new in Python I hope you can help me.
I've tried many different programs, but I didn't get any results. Here is one:
import requests
url ="https://bboxxltd.atlassian.net/rest/servicedeskapi/servicedesk/CMS/queue/213/issue"
auth='XXXXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXXX'
r = requests.get(url, auth=(auth))
data = r.json().get('summary')
print(data)
Output: None
I wanted to have in the "summary", in this example:
For example:
Output:
summary:REQUEST FOR DATA
When you do for in in data, the i variable will take the value of the keys of data, one at the time. So normally you would do data[i] inside the for i in data.
If id is a high level attribute of data, you can simply do data['id'] outside the for loop. Anyways, this all depends on the structure of the returned JSON.
From the screenshot, you are getting:
KeyError: 'summary'
Which means that data is not an Array but an Object. You need to go down the Object further in order to reach the Array you are looking for. You need to inspect the data object; one good way to do this is to call print(data.keys()), this way, you'll find the attributes that you can access from data, until you get the array you are after.
Once you know the structure of the response
# It looks like they array is multiple levels
# inside data, so it may look like this:
issues = data[key1][key2]...[keyn]
for issue in issues:
if issue['id'] == issue_id:
...
As Pynchia points out,
if you can reach the elements of the array correctly,
i.e. i is correct,
then access summary via the fields key:
print(i['fields']['summary'])
Also, please post text rather than images.
Images can't be searched and therefore aren't useful to future readers.
You're asking us to volunteer our time for free to solve your problem, and you should make it as easy as possible for us to do so.
Why not upload images of code on SO when asking a question?
EDIT
Your question is unclear.
It is straightforward to ask for all the elements it contains:
for k, v in i['fields']:
print(f'The value of {k} is {v}.')
In your example, one of those k keys will be 'summary'.

How to concatenate two functions into one function?

I have a set of data that is generated:
=((E31/320)^2)/(2+(E31/380))
=((E32/320)^2)/(2+(E32/380))
=((E33/320)^2)/(2+(E33/380))
...
I want to create a sum of these, but I don't want to just SUM them together; I want to write a function that put these together. I came up with this row:
=SUMPRODUCT(((ROW(E1:INDEX(E31:E63;C34)))/320)^2/(2 + (E31:E63/380)))
The problem with this line is it seems to overdo the whole thing. I need to somehow use one variable for the both E31:E63 intervals, because it will otherwise loop through the second E31:E63 n-times, instead of using the same value.
As I see it, there are two solutions.
Write the data in columns, but using the first solution
Write the function, but try to find something that makes the two E31:E63 work as one variable.
I want to implement the second option.
I believe
=SUMPRODUCT(((E31:E63/320)^2)/(2+(E31:E63/380)))
Will do what you want.

Add Dictionary Keys and Values to Redis List

I am trying to add the current dictionary to a Redis list using a dictionary comprehension and then to print out the first (aka current) keys and values of that list. I say current because this is a process I will be continuing with a while loop to have the list building over time, but I have to always access the first keys/values.
I am sure I am totally butchering this, but this is what I have:
adict = {"a":1,"b":2,"c":3}
{rserver.rpush("list",value) for value in adict}
print(float(rserver.lindex("list",0)))
I need to get a list of both keys and values back.
Help would be MUCH appreciated. Thanks!
I am not quite positive on what your redis-list should contain (please include your expected result in the question), but assuming it should at the end of inserts look something like this ["a:1", "b:1", "c:1"], you can achieve this with
adict = {"a":1,"b":2,"c":3}
for key,value in adict.items():
rserver.rpush("list", ":".join([key, value]))
print(float(rserver.lindex("list",0))) #>>> "a:1"
(as you have not included what interface rserver exactly is, it is a bit hard to guess on its exact behavior)

Checking if an element exists without losing time in Capybara

I like to keep things DRY, that's why I want to have the following in one of my steps:
if first(:css, "#blabla") != nil
find_by_id(blabla).click
end
find_by_id(....)
....
This means, that it will look for a certain element, and if it exists, it will click on it. If not, I will not lose time (my default wait time is 20 secs, which will be used if I put find instead of first there.
The main issue is that I don't want to lose time when checking for a certain element in this case, but I am also wondering if this is a good approach.
I see the issue that your code does unnecessary second query to browser (you already have first(:css, "#blabla") so no need to do find_by_id(blabla))
I propose you to find element using one query:
el = first('#blabla')
el.click unless el.nil?
Note that there is no losing time here as first doesn't block.
However, first doesn't check that there no other elements on the page. You can add :maximum to check it:
el = first('#blabla', maximum: 1)
el.click unless el.nil?
When you're using #find to decide on element presence, you should reduce the wait time for just that call:
if page.has_css?('#blabla', wait: 0)
# Do something
end
However, in your special case, the suggested solution is even better, because it saves you multiple "find" calls for the same element.

Resources