I am trying to make a loop that populates 9 radio buttons, each with 4 options, and placed in a grid. The following code words, however, I can select more than one option for each question. My understanding is that for each variable I should only be able to select one. Is the loop constructed incorrectly?
MODES = [
('dep_name1', 'dep_but1', 2, "Not at all", 'op1'),
('dep_name2', 'dep_but2', 3, "Several days", 'op2'),
('dep_name3', 'dep_but3', 4, "More than half the days", 'op3'),
('dep_name4', 'dep_but4', 5, "Nearly everyday", 'op4'),
]
for r in range (2, 11):
for dep_name, button, c, text, b_val in MODES:
button = tk.StringVar()
# button.set("L")
dep_name = tk.Radiobutton(window, text=text, variable=button, indicatoron=0, value=b_val).grid(row=r, column=c, sticky='e')
window.columnconfigure("all", weight=1)
window.rowconfigure("all", weight=1)
window.mainloop()
You are creating one variable for each radiobutton. You should have one variable for each group of radiobuttons.
Since the variable can have only one value at a time, tkinter uses the variable to know which radiobuttons belong to a logical group. That is to say, all radiobuttons with the same variable are one logical group of buttons.
Here's one way to do it:
vars = []
for r in range (2, 11):
var = tk.StringVar(value="op1")
vars.append(var)
for dep_name, button, c, text, b_val in MODES:
tk.Radiobutton(window, text=text, variable=var, indicatoron=0, value=b_val).grid(row=r, column=c, sticky='e')
This example saves the variables to a list so that you can reference them by index elsewhere in your code.
I don't quite understand what dep_name and dep_button are supposed to represent. In both cases you initialize them from MODES, but then you change them inside the loops. Regardless, the point is that you need to create the variable just inside the outer loop so that you create one variable for each group of radiobuttons.
Related
I do have a specific tradingview, pinescript command structure I want to maintain, and this includes strategy related arguments as well.
(since I know matlab, I will start with this). In matlab nomenclature, if you have a string array, you can do the following
array = ['dog'; 'cat']
and you can call
array(1) (to display 'dog', or array(2) to display 'cat' ...etc. And if you want to assign it to a new variable, you can do it as
new = array(1); %etc.....
In pinescript what I am trying to do is the following
orderCondition = array.new_string(4)
array.insert(orderCondition, 1, 'open')
array.insert(orderCondition, 2, 'open_new')
array.insert(orderCondition, 3, 'close')
array.insert(orderCondition, 4, 'close_old')
So in this array I am hoping I have something like
[ 'open'; 'open_new'; 'close'; 'close_old' ]
The critical part is in the assignment section. What I want to achieve is the following. I want to declare the first two parts of the array in one strategy comment, and the remaining two in the other, like
strategy.entry("LE", strategy.long, comment=orderCondition[1,2])
strategy.entry("LE", strategy.long, comment=orderCondition[3,4])
so that I group them. And not only that I am also hoping to be able to read those in strategy alert window, as
{{strategy.comment[1-2]}}
{{strategy.comment[3-4]}}
Is this possible? And if possible how can I achieve this? Thank you for your time.
I have added the following two equations to conditional formatting:
Green: =IF(REGEXMATCH(VLOOKUP(X2, INDIRECT("DEALS!$A$2:F"),5, FALSE), "Likes"), R2>=VLOOKUP(X2, INDIRECT("DEALS!$A$2:F"), 4, FALSE), T2>=VLOOKUP(X2, INDIRECT("DEALS!$A$2:F"), 4, FALSE))
Red: =IF(REGEXMATCH(VLOOKUP(X2, INDIRECT("DEALS!$A$2:F"),5, FALSE), "Likes"), NOT(R2>=VLOOKUP(X2, INDIRECT("DEALS!$A$2:F"), 4, FALSE)), NOT(T2>=VLOOKUP(X2, INDIRECT("DEALS!$A$2:F"), 4, FALSE)))
The colors should change accordingly depending on whether the target (views in this case) has been met or not.
Below I have also added the equation into the cells to check if the logic is correct, which it appears to be (left = green logic, right = red logic).
For whatever reason, the first row, despite the target not being met, has decided to select the green color. The row below that is doing the complete opposite. And to top it all off, the last two rows are not selecting a color at all even though I have applied the conditional formatting to the entire column:
I am also experiencing weird behavior when dragging equations within this P column, but do not see this same behavior in other columns that also use conditional formatting:
https://i.gyazo.com/5e002e3d08e8337591573b81d9fc92e2.mp4
This has left me completely baffled, and I am not sure what is going on since the equation's logic does not appear to be the issue.
Appreciate any help I can get with this issue!
For reference, here is the other sheet that the VLOOKUP() function is grabbing from:
do not lock ($) references inside INDIRECT. if stuff is between double quotes it's a text string, not an active reference, and text strings are not affected by dragging.
for green use:
=IF(REGEXMATCH(VLOOKUP(Z2, INDIRECT("DEALS!A2:F"), 5, 0), "Likes"),
R2>=VLOOKUP(Z2, INDIRECT("DEALS!A2:F"), 4, 0),
T2>=VLOOKUP(Z2, INDIRECT("DEALS!A2:F"), 4, 0))
for red use:
=IF(REGEXMATCH(VLOOKUP(Z2, INDIRECT("DEALS!A2:F"), 5, 0), "Likes"),
NOT(R2>=VLOOKUP(Z2, INDIRECT("DEALS!A2:F"), 4, 0)),
NOT(T2>=VLOOKUP(Z2, INDIRECT("DEALS!A2:F"), 4, 0)))
demo sheet
update:
don't drag anything. use this in P2:
=ARRAYFORMULA(IFNA(TEXT(VLOOKUP(Z2:Z,DEALS!A2:F,4,0),
"#,###,##0")& " " &VLOOKUP(Z2:Z,DEALS!A2:F,5,0)))
I am trying to make an easy to use button API in Lua with ComputerCraft, and I'm having some trouble. When I do:
os.loadAPI("button")
action=function()
term.clear()
term.setCursorPos(1,1)
print("Hello!")
end
button.newButton("B1",5,5,20,10)
button.drawButton("B1",colors.orange,colors.white)
button.onClick("B1",action,true)
Nothing happens, it doesn't even draw the colors. I have done tests, and when I store something like colors.white as a variable, then print the variable, it returns the number code of that color, which comes from the colors API. Here is what I have:
--to use the newButton function, do this:
--button.newButton(exampleButton)
--to use onClick function, create a variable like this:
--exampleFunc=function()
--(code)
--end
--Then call onClick with the same variable:
--button.onClick(exampleButton,exampleFunc)
buttons={}
xPos=0
yPos=0
function removeButton(buttonName)
for key, fields in pairs(buttons) do
if key == buttonName then
table.remove(button,buttonName)
else
print("ERROR: button name not available")
end
end
end
function onClick(buttonName,action,boolean)
for key, fields in pairs(buttons) do
if boolean then
testClick(action)
end
end
end
function drawSeparateButton(x,y,w,h,outLineColor,fillColor)
if key == buttonName then
x=buttons[buttonName]["x"]
y=buttons[buttonName]["y"]
w=buttons[buttonName]["w"]
h=buttons[buttonName]["h"]
paintutils.drawBox(x,y,x+(w-1),y+(h-1),outLineColor)
paintutils.drawFilledBox(x+1,y+1,x+(w-2),y+(h-2),fillColor)
end
end
function testClick(action)
for key, fields in ipairs(buttons) do
x=buttons[buttonName]["x"]
y=buttons[buttonName]["y"]
w=buttons[buttonName]["w"]
h=buttons[buttonName]["h"]
x2=x+(w-1)
y2=y+(h-1)
button,xPos,yPos=os.pullEvent("mouse_click")
if xPos>=x and xPos<=x2 and yPos>=y and yPos<=y2 then
action()
end
end
end
function newButton(buttonName,X,Y,W,H)
buttons[buttonName] = {x=X,y=Y,w=W,h=H}
end
function drawButton(buttonName,outLineColor,fillColor)
for key, fields in ipairs(buttons) do
if key == buttonName then
x=buttons[buttonName]["x"]
y=buttons[buttonName]["y"]
w=buttons[buttonName]["w"]
h=buttons[buttonName]["h"]
x2=x+w-1
y2=y+h-1
x3=x+1
y3=y+1
x4=x+w-2
y4=y+h-2
paintutils.drawBox(x,y,x2,y2,outLineColor)
paintutils.drawFilledBox(x3,y3,x4,y4,fillColor)
elseif key ~= buttonName then
print("Button name not availabel")
end
end
end
I just need to be able to store a color like colors.white in a variable and have it returned as colors.white, and not the color code. I also need to be able to check which button is clicked and run a function specified by the user when one of the buttons are clicked.
I'm going to walk through your prototype code and point out some errors I see and also try to answer your question. I'm going to assume that you want to set a key value in a table to an array and access that externally.
A quick and short answer to your question is that you can store tables within tables and access them through keys or indices. A design change I would make, however, is to store your exampleFunc as a member of each button table to associate it with a specific button.
Example:
buttons = {}
buttons.playButton = {x=0, y=0, w=10, h=10, func=function() return end}
buttons.quitButton = {x=0, y=30, w=10, h=10, func=function() return end}
...
buttons.quitButton.x = 10
buttons.playButton.func()
Tables have a key-value structure, where keys can be strings or numbers. There are multiple ways to access an array using a key depending on the data type of the key.
For example, instead of writing buttons.quitButton.x = 10 we could've written buttons["quitButton"].x = 10 or buttons["quitButton"]["x"] = 10 or buttons.quitButton["x"] = 10.
This page is a great starting point for learning about Lua's tables.
According to this page, os.pullEvent() is blocking, and you will only be able to check if one button is clicked per mouse click. Consider looping through your buttons table and checking every button to see if the mouse falls within its rectangular bounds. Once you find which button the mouse clicked, you can call its func member. While we're still discussing this method, the while true do loop is completely unnecessary.
function removeButton(buttonName)
for buttonName in pairs(button) do
...
function newButton(buttonName)
state=true
for buttonName in pairs(buttons) do
...
You may come from a Python background where the statement if element in list exists but Lua has no such statement. The for loops you use are looping through every member of the list. You also aren't capturing all of the variables returned by the pairs() function. A fix for that would look something like the following:
function buttonFunction(buttonName)
for key, fields in pairs(buttons) do
if key == buttonName then
...
end
end
There are multiple instances where you refer to the variable button when you mean buttons.
I have a collection with 50 objects.
Each object contains several things but we have at least
OrgNr, Register_Id, Name, MailDate, Subject
for each object.
Assume these have the following values
OrgNr = "2556745-8145"
Register_Id = "1001"
Name = "Thailaan Asian Market"
MailDate = "2016-05-23 13:01:20"
Subject = "LPP failure"
So when I read the first object and want to add the above five fields to the ListBox I want it to be displayed like this and as one column.
2556745-8145/1001
Thailaan Asian Market
2016-05-23 13:01:20
LPP failure
So all these five fields is like one item and should be clickable.
I have been looing everywhere but the only example I have found is this
http://www.codeproject.com/articles/2695/an-editable-multi-line-listbox-for-net.
I can't use this example because I want to be able to control when I want to add CR LF which is after each written field value
//Tony
I need to know whether it is possible to add values to a ComboBox at different times.
ObservableList<String> options1
= FXCollections.observableArrayList(
"Civil (CE)", "Computer (CT)", "Electrical (EEE)", "Electronics (ELS)",
"Mechanical (ME)");
comboBox1 = new ComboBox(options1);
comboBox1.setPrefSize(280, 30);
This is my code of ComboBox. In it I added 5 values at a time. But is it possible to add each value at different times, for example, add values one by one in a while loop. I tried it and the result was that each value overlapped previously added values, and as a result only one value was present in the ComboBox at the end. This the code with while loop -
while (rs.next()) {
subject = rs.getString("subname");
ObservableList<String> options1 = FXCollections.observableArrayList(subject);
comboBox1 = new ComboBox(options1);
}
Can I add values to ComboBox at different times, one after another, without overlapping the previous value ?
Yes, you can add/remove items at any moment using getItems()
comboBox1.getItems().add("Beer");
comboBox1.getItems().add("Whiskey");
comboBox1.getItems().add("Water");
or directly updating list:
options1.add("Milk");