Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Random Name from an Array


  • Please log in to reply
10 replies to this topic
ironcates
  • Members
  • 32 posts
  • Last active: Oct 14 2013 02:32 PM
  • Joined: 10 Aug 2011
I'm new to AHK and have done very little programming in general.

I read in a list of student names one from each period from a text file. I have the Gui looking good but need a little help connecting everything.

My idea is to take roll with the check boxes then during the class when I hit the button it will choose a random student from those checked. Ideally, I'd like the Button itself to display the name which maybe harder that just popping up a MsgBox but nicer to use in rapid succession.



FileRead, StudentList1, %A_ScriptDir%\StudentList1.txt
FileRead, StudentList3, %A_ScriptDir%\StudentList3.txt


StringSplit, StudentArray1, StudentList1, `,
StringSplit, StudentArray3, StudentList3, `,


Gui, Add, Tab, x10 y10 w600 h350 , P1|P3/4
Gui, Tab, P1
Gui, Add, Button, x411 y54 w190 h290 , Random
Gui, Add, CheckBox, x41 y54 w180 h20 , % StudentArray11
Gui, Add, CheckBox, x41 y74 w180 h20 , % StudentArray12
Gui, Add, CheckBox, x41 y94 w180 h20 , % StudentArray13
Gui, Add, CheckBox, x41 y114 w180 h20 , % StudentArray14
Gui, Add, CheckBox, x41 y134 w180 h20 , % StudentArray15
Gui, Add, CheckBox, x41 y154 w180 h20 , % StudentArray16
Gui, Add, CheckBox, x41 y174 w180 h20 , % StudentArray17
Gui, Add, CheckBox, x41 y194 w180 h20 , % StudentArray18
Gui, Add, CheckBox, x41 y214 w180 h20 , % StudentArray19
Gui, Add, CheckBox, x41 y234 w180 h20 , % StudentArray110
Gui, Add, CheckBox, x41 y254 w180 h20 , % StudentArray111
Gui, Add, CheckBox, x41 y274 w180 h20 , % StudentArray112
Gui, Add, CheckBox, x41 y294 w180 h20 , % StudentArray113
Gui, Add, CheckBox, x41 y314 w180 h20 , % StudentArray114
Gui, Add, CheckBox, x41 y334 w180 h20 , % StudentArray115
Gui, Add, CheckBox, x221 y54 w180 h20 , % StudentArray116
Gui, Add, CheckBox, x221 y74 w180 h20 , % StudentArray117
Gui, Add, CheckBox, x221 y94 w180 h20 , % StudentArray118
Gui, Add, CheckBox, x221 y114 w180 h20 , % StudentArray119
Gui, Add, CheckBox, x221 y134 w180 h20 , % StudentArray120
Gui, Add, CheckBox, x221 y154 w180 h20 , % StudentArray121
Gui, Add, CheckBox, x221 y174 w180 h20 , % StudentArray122
Gui, Add, CheckBox, x221 y194 w180 h20 , % StudentArray123
Gui, Add, CheckBox, x221 y214 w180 h20 , % StudentArray124
Gui, Add, CheckBox, x221 y234 w180 h20 , % StudentArray125
Gui, Add, CheckBox, x221 y254 w180 h20 , % StudentArray126
Gui, Add, CheckBox, x221 y274 w180 h20 , % StudentArray127
Gui, Add, CheckBox, x221 y294 w180 h20 , % StudentArray128
Gui, Add, CheckBox, x221 y314 w180 h20 , % StudentArray129
Gui, Add, CheckBox, x221 y334 w180 h20 , % StudentArray130

Gui, Tab, P3/4
Gui, Add, Button, x411 y54 w190 h290 , Button
Gui, Add, CheckBox, x41 y54 w180 h20 , % StudentArray31
Gui, Add, CheckBox, x41 y74 w180 h20 , % StudentArray32
Gui, Add, CheckBox, x41 y94 w180 h20 , % StudentArray33
Gui, Add, CheckBox, x41 y114 w180 h20 , % StudentArray34
Gui, Add, CheckBox, x41 y134 w180 h20 , % StudentArray35
Gui, Add, CheckBox, x41 y154 w180 h20 , % StudentArray36
Gui, Add, CheckBox, x41 y174 w180 h20 , % StudentArray37
Gui, Add, CheckBox, x41 y194 w180 h20 , % StudentArray38
Gui, Add, CheckBox, x41 y214 w180 h20 , % StudentArray39
Gui, Add, CheckBox, x41 y234 w180 h20 , % StudentArray310
Gui, Add, CheckBox, x41 y254 w180 h20 , % StudentArray311
Gui, Add, CheckBox, x41 y274 w180 h20 , % StudentArray312
Gui, Add, CheckBox, x41 y294 w180 h20 , % StudentArray313
Gui, Add, CheckBox, x41 y314 w180 h20 , % StudentArray314
Gui, Add, CheckBox, x41 y334 w180 h20 , % StudentArray315
Gui, Add, CheckBox, x221 y54 w180 h20 , % StudentArray316
Gui, Add, CheckBox, x221 y74 w180 h20 , % StudentArray317
Gui, Add, CheckBox, x221 y94 w180 h20 , % StudentArray318
Gui, Add, CheckBox, x221 y114 w180 h20 , % StudentArray319
Gui, Add, CheckBox, x221 y134 w180 h20 , % StudentArray320
Gui, Add, CheckBox, x221 y154 w180 h20 , % StudentArray321
Gui, Add, CheckBox, x221 y174 w180 h20 , % StudentArray322
Gui, Add, CheckBox, x221 y194 w180 h20 , % StudentArray323
Gui, Add, CheckBox, x221 y214 w180 h20 , % StudentArray324
Gui, Add, CheckBox, x221 y234 w180 h20 , % StudentArray325
Gui, Add, CheckBox, x221 y254 w180 h20 , % StudentArray326
Gui, Add, CheckBox, x221 y274 w180 h20 , % StudentArray327
Gui, Add, CheckBox, x221 y294 w180 h20 , % StudentArray328
Gui, Add, CheckBox, x221 y314 w180 h20 , % StudentArray329
Gui, Add, CheckBox, x221 y334 w180 h20 , % StudentArray330
; Generated using SmartGUI Creator 4.0
Gui, Show, x231 y175 h370 w620, New GUI Window
Return

GuiClose:
ExitApp

StudentList1.txt
Adam Ant,Ben Button,Clark Kent,Dave Deerborn,Elmer Edwards,Fred Foster,Grant Green,Howard Holmes,Ida Ingle,Jack Johnson,Kal-El from Krypton,Lois Lane,Martian Manhunter,Nate North,Owen Olson,Peter Parker,Quincy Qualls,Ronald Reagan,Scuba Steve,Terry Thatcher,Uma Usher,Veronica Vice,Wally World,Xavier Xman,Yolanda Yates,Zack Zimira

StudentList3.txt
Alex Ant,Ben Button,Clark Kent,Dave Deerborn,Elmer Edwards,Fred Foster,Grant Green,Howard Holmes,Ida Ingle,Jack Johnson,Kal-El from Krypton,Lois Lane,Martian Manhunter,Nate North,Owen Olson,Peter Parker,Quincy Qualls,Ronald Reagan,Scuba Steve,Terry Thatcher,Uma Usher,Veronica Vice,Wally World,Xavier Xman,Yolanda Yates,Zack Zimira
Thanks for any help. My students and I thank you!

engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005
thanks for posting your example code!

I'd start by giving your button a variable name (v option, such as vMyButton), so that you can reference it with other commands. Also give it a g-label (g option) so you can run code when it is pressed.

then you can use GuiControl to set the text of that button.

the brute force way is to have it generate a random number and if that box is checked (GuiControlGet returns a 1 for checkboxes), then set the text of the button (GuiControl) to the name of the student (from the array), else, try another random number.

you may need to add a v option to all your checkboxes too.

You can also have it automatically add the right number of checkboxes based on the length of the list. Let me know if you want help with any of these things (by replying to this thread )

ironcates
  • Members
  • 32 posts
  • Last active: Oct 14 2013 02:32 PM
  • Joined: 10 Aug 2011
Yeah, I tried the v option in the CheckBox code and the text doesn't print out. I'm not understanding how to use the g-label. I haven't been able to find an example in the Gui documentation.

If I put this (code below) in for the checkbox, I get "Error at line 15..Line Text: vSA11, % StudentArray11..Error: This Parameter contains a variable name missing its ending percent sign." But if I throw a percent after StudentArray11 the text won't show up on the CheckBox.

Gui, Add, CheckBox, x41 y54 w180 h20 , vSA1, % StudentArray11


An example should get me headed in the right direction.

[/code]

ironcates
  • Members
  • 32 posts
  • Last active: Oct 14 2013 02:32 PM
  • Joined: 10 Aug 2011
BTW

I did the button changing trick in python before without the checkbox front end to say which students were present. The random.choice() function was my savior.

<PYTHON CODE>
import random
from Tkinter import *
import tkMessageBox

namelist=[]
f=open('StudentList.txt','r')
for name in f:
    namelist.append(name)   

class App:

    def __init__(self, master):

        frame = Frame(master)
        frame.pack()

        self.chooser = Button(frame, width=20, height=10, text="Random Student", command=self.choose_student)
        self.chooser.pack(side=LEFT)

    def choose_student(self):
        chosen=random.choice(namelist)
        self.chooser.configure(text=chosen)

root = Tk()
app = App(root)
root.mainloop()
[/code]

engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005
v and g are options just like x, y, w, and h. you have an extra comma.

for a button, a g option specifies what label you want launched when the button is pressed. If you don't specify one, it defaults to Button%ButtonText%, so you can do it either way:
Gui, Add, Button, w100, Go

...

ButtonGo:
Gui, Add, Button, w100 gDoSomething, Go

...

DoSomething:


engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005
So I got a little bored, and wrote a script for you that generates everything programatically:

#NoEnv
#SingleInstance force

	Loop, %A_ScriptDir%\*.txt
	{
		thisTab := A_Index	;Just a number
		thisFile := A_LoopFileName	;What file?
		
		;Create Student lists
		FileRead, StudentList%thisTab%, %thisFile%	
		StringSplit, StudentArray%thisTab%, StudentList%thisTab%, `n, `r
		
		studentsOnTab%thisTab% := StudentArray%thisTab%0
		
		;Add tab to the list
		tabName%thisTab% := SubStr(thisFile, 1, StrLen(thisFile) - 4)	;cut off the .txt
	
	}
	numberOfTabs := thisTab


	;OK, all the data is collected, now build a Gui
	;The stuff we need to do once
	Gui, Add, Button, x420 y54 w180 h290 gPickStudent vRandomButton, Random Selection

	;build list of tabs
	Loop, %numberOfTabs%
	{
		thisTab := A_Index
		tabNames := tabNames . "|" tabName%thisTab%
	}
	tabNames := SubStr(tabNames, 2)	;Cut off the leading |
	Gui, Add, Tab, AltSubmit vTabControl x10 y10 w400 h350 , %tabNames%
	

	;now populate the tabs
	Loop, %numberOfTabs%
	{
		thisTab := A_Index
		Gui, Tab, % tabName%thisTab%

		;set up positioning
		startX := 40
		startY := 55
		incX := 180
		incY := 20
		maxY := 330
	
		currentX := startX
		currentY := startY
		
		Loop, % studentsOnTab%thisTab%
		{
			thisCheckbox := thisTab . A_Index		;tab number concatenated with checkbox number
			;check that there is a name in this element - blank names will create blank lines in the GUI
			if StrLen(StudentArray%thisCheckbox%) > 1
			{
				;add the student checkbox (prechecked, so you only have to uncheck absent students
				Gui, Add, Checkbox, Checked x%currentX% y%currentY% vCheckbox%thisCheckbox%, % StudentArray%thisCheckbox%
			}

			;increment the position
			currentY := currentY + incY
			if (currentY > maxY)
			{
				currentX := currentX + incX
				currentY := startY
			}
		
		}

	}

	Gui, Show
Return

GuiClose:
	ExitApp


PickStudent:
	;find out what tab we are on
	GuiControlGet, selectedTab, , TabControl
	
	;loop
	Loop
	{
		;pick a student
		Random, selection , 1, % studentsOnTab%selectedTab%
		
		;if the student is present (checked), set the button text
		GuiControlGet, checkedStatus, , Checkbox%selectedTab%%selection%
		if (checkedStatus)
		{
			GuiControl, , RandomButton, % "Random Selection`n" StudentArray%selectedTab%%selection%
			Return
		}
	}
Return

This is completely tested.
I made it so that it reads the folder for .txt files. The name of the file becomes the name of the tab. The students in the file become checkboxes. I made them separated by newlines, so it is more readable, and can be copy/pasted from excel or other table formats
Blank students are allowed, if you wanted to clump the students by row, for example
Checkboxes are checked by default, so you only have to uncheck absent students (they are supposed to be there by default, right :))

Let me know what you think, or if you want me to explain something :)

Odlanir
  • Members
  • 775 posts
  • Last active: Mar 06 2014 11:02 AM
  • Joined: 07 Aug 2011
you must give each checkbox a unique variable name, wich expose the checkbox state ( checked or not ).

for example:
Gui, Add, CheckBox, x41 y54 w180 h20 vSA1, % StudentArray11
Gui, Add, CheckBox, x41 y74 w180 h20 vSA2, % StudentArray12
Gui, Add, CheckBox, x41 y94 w180 h20 vSA3, % StudentArray13
...
...
Gui, Add, CheckBox, x221 y314 w180 h20 vSA29, % StudentArray129
Gui, Add, CheckBox, x221 y334 w180 h20 vSA30, % StudentArray130
you also need a g-label in the button option
Gui, Add, Button, x411 y54 w190 h290 gLabelX , Button
.. and the code to be executed when you press the button
LabelX:
	tempArray := Object()

	loop, 30
	{
		GuiControlGet, ch, ,SA%A_index%
			IfEqual, ch, 1
				tempArray.Insert(StudentArray1%A_Index%)
	}
	Random, rnd, 1, tempArray.MaxIndex()
	msgbox % tempArray[rnd]
return

Regards

engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005

LabelX:
	tempArray := Object()

	loop, 30
	{
		GuiControlGet, ch, ,SA%A_index%
			IfEqual, ch, 1
				tempArray.Insert(StudentArray1%A_Index%)
	}
	Random, rnd, 1, tempArray.MaxIndex()
	msgbox % tempArray[rnd]
return

note that this requires AHK_L.

Also, IfEqual bugs me (I'm not that oldschool)
I'd suggest "if (ch = 1)" as being more clear

ironcates
  • Members
  • 32 posts
  • Last active: Oct 14 2013 02:32 PM
  • Joined: 10 Aug 2011
You guys rock! I'll have to work on this tonight after school.

ironcates
  • Members
  • 32 posts
  • Last active: Oct 14 2013 02:32 PM
  • Joined: 10 Aug 2011
engunneer,

I played around with the script a lot. I'm really in your debt!

I love how you made the default checked (something I had thought about but hadn't even asked for).

Your solution is very elegant and very helpful for me to understand what AHK can do and how to use the syntax.

I've done a little tweaking to make the font larger and I moved the button to the bottom so the names will show large and all the way across. I have a couple other ideas but I think I can figure them out.

Thanks again.

engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005
no problem!

If you have questions about some of what i did, or how to add you upgrades, let me know.