I have a canvas with 30 labels, the text is updated (always different) using a button. I have 2 questions:
1.-why the command time.sleep doesn't work as I expect?
i = 0
while len(words) >= 1:
if i = 1:
canvas.itemconfig(label1, text = "text")
time.sleep(3)
if i = 2:
canvas.itemconfig(label2, text = "text")
time.sleep(3)
if i = 3:
canvas.itemconfig(label3, text = "text")
time.sleep(3)
if i = 4:
canvas.itemconfig(label4, text = "text")
time.sleep(3)
if i = 5:
canvas.itemconfig(label5, text = "text")
time.sleep(3)
i+= 1
And so on until 30, I want to set the label and then wait 3 seconds to set the next label and so on, but the code waits 15 seconds and sets all the labels at the same time at the end.
2.-is there a way to refer to my labels using "for" to avoid writing 30 lines? All my labels are called label1, label2, label3... I have done this in other language with something like this
For i in range(1,31):
canvas.itemconfig(label[i], text = "text")
Sorry if I wrote something wrong since I did it in my phone
When you use sleep in tkinter, you're freezing the program so nothing happens. If you want to make a change after a given time, use the after method.
root.after(1000, myfunction) # call myfunction after 1 second
As for the label updates, you are correct. Creating a label array would make it easier.
With the current label names, you can add them all to a list then iterate the list.
lbllst = [label1, label2, label3, ......]
for lbl in lbllist:
canvas.itemconfig(lbl, text = "text")
Related
What is the easiest way to get the whole line of text having line number/where the mouse caret is? (In currently open document.)
I'm getting line number using:
DTE dte = Package.GetGlobalService(typeof(DTE)) as DTE;
int line = ((EnvDTE.TextSelection)dte.ActiveDocument.Selection).ActivePoint.Line;
also using similar approach I can get selected text:
string line = ((EnvDTE.TextSelection)dte.ActiveDocument.Selection).Text;
but I'm struggling to find anything that could be useful.
To get the whole line of text where the caret is:
var activePoint = ((EnvDTE.TextSelection)dte.ActiveDocument.Selection).ActivePoint;
string text = activePoint.CreateEditPoint().GetLines(activePoint.Line, activePoint.Line + 1);
I have a picture box and I print contents in it. I want to know the exact textwidth of the text in millimeters. But I get wrong value. here is my code
me.scalemode = vbmillimeters
picturebox.scalemode = vbmillimeters
picturebox.fontname = "Arial"
picturebox.fontsize = 12
debug.print textwidth("AB.C.D.E. FGHIJKLMN")
When i measure in the printout in paper it is 48 mm
but it shows 32.97mm
please help me where am wrong.
Thanks in advance
If you need the width of the text printed to the picture box, use:
PictureBox.textwidth("AB.C.D.E. FGHIJKLMN")
What you are actually doing: textwidth("AB.C.D.E. FGHIJKLMN") is mesuring the same text printed to the Form (Me).
Doing like this would be less error-prone:
Dim TextWidth as Single
With PictureBox
.ScaleMode = vbMillimeters
.FontName = "Arial"
.FontSize = 12
TextWidth = .TextWidth("AB.C.D.E. FGHIJKLMN")
End With
because if you are then switching to paper, you can also easily switch context:
With SelectedPrinter....
This question already has an answer here:
How to generate random numbers without repetition in Swift? [duplicate]
(1 answer)
Closed 7 years ago.
In my application, I use something like this to get random text on my label, except in my main let randomNumbercode, in xCode, it has over 300 cases, to much to paste in here.:
let randomNumber = Int(arc4random_uniform(23))
var textLabel = "" as NSString
switch (randomNumber){
case 1:
textLabel = "Kim."
break
case 2:
textLabel = "Phil."
break
case 3:
textLabel = "Tom"
break
case 4:
textLabel = "Jeff"
break
default:
textLabel = "Austin"
}
self.randomLabel.text = textLabel as String
But the problem is, that sometimes it shows the same text on the label 5-6 times, and other cases is not even used yet, because it choose randomly. So how can I choose randomly, but if case example case 1 is already shown, it wont show up again, until all other cases has been shown.
Have an array of Names instead of a gigantic switch case:
var names = ["Kim.", "Phil.", "Tom", "Jeff", "Austin"] // and all your remaining names
let originalNames = names
func getRandomName() -> String {
if (names.count == 0) {
names = originalNames
}
let randomNumber = Int(arc4random_uniform(UInt32(names.count)))
return names.removeAtIndex(randomNumber)
}
This ensures every name gets printed before starting from the beginning again. The sample output is:
Tom, Kim., Austin, Phil., Jeff
and then it starts again
Austin, Jeff, Phil. ...
Finally put something like the following wherever it fits your need:
self.randomLabel.text = getRandomName()
How do I find all cells containing a text value such as merchant_id and change the background color of those cells to a specific color?
This macro will use the current selected range and check each cell for merchant_id. Then mark it maroon if true. To pick a particular color, the best way is to record a macro and see what values it creates. Take those numbers and replace the contents of the With block
Sub MarkCellsInSelection()
For Each c In Selection
If c.Value = "merchant_id" Then
With c.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent2
.TintAndShade = 0.399975585192419
.PatternTintAndShade = 0
End With
End If
Next
End Sub
I want to add the lable box value in vb6
Label1 = 200
Label2 = 500
'Adding
Label3 = Label1 + Label2
'Showing output as
Label3 = 200500
I want add the 2 values
Expected Output
Label3 = 700
What was the problem in my code
Need code help
The two answers are correct, but neither of it explain to you why this happens. VB 6 (or 5 or 4 or 3) has a default property for the controls. In the case of the label, the default property is caption. Since caption is a string, and string can be concatenate using & or +, VB pickup the type and then it does the math (in this case, concat).
Label3= val(Label1) + val(Label2)
This works good.. Also you can use Cint or any other convert to number function.
long time haven't worked with VB6 but try
Label3.caption = val(Label1.caption) + val(Label2.caption)
Do something like this:-
textbox3.text = val(textbox1.text) + val(textbox2.text)