Jump to content

celcius/ fahrenheit converter


XY16
 Share

Recommended Posts

check out my celcius / fahrenheit converter.

;celcius-fahrenheit converter

;set include requirements

#include <GUIConstantsEx.au3>

#include <string.au3>

;disable the auto-it tray icon

#NoTrayIcon

;set special options

Opt('MustDeclareVars', 1)

Opt("GUICoordMode", 2)

Opt("GUICloseOnESC", 0)

;execute the main gui function

MainGui()

;set the main gui function

func MainGui()

;declare button variables

Local $Button_1, $Button_2, $Button_3, $msg

;create the gui

GUICreate("Levtec celcius-fahrenheit converter")

;set the first button to perform conversions

$Button_1 = GUICtrlCreateButton("Convert", 0, -1)

;set the second button to visit the levtec web site

$Button_3 = GUICtrlCreateButton("Web site", 0, -1)

;set the third button to exit the program

$Button_3 = GUICtrlCreateButton("Exit", 0, -1)

;now create the gui

GUISetState()

;Run the GUI until the dialog is closed

While 1

;show the dialog on screen

$msg = GUIGetMsg()

;start a select statement

Select

;set what happens when the app is closed

Case $msg = $GUI_EVENT_CLOSE

;display a message box

MsgBox(64, "Information", "Thanks for using the levtec celcius-fahrenheit converter." & @CRLF & "Click OK to close.")

;exit the loop and distroy the gui

ExitLoop

;set what happens when the convert button is clicked

Case $msg = $Button_1

;destroy the first gui

GUIDelete()

;open the converter gui

Conv()

;exit the loop

ExitLoop

;set what happens when the web site button is clicked

Case $msg = $Button_2

;display a message box

MsgBox(64, "Information", "You chose to visit the levtec web site." & @CRLF & "This action requires an active internet connection and an installed browser." & @CRLF & "Click OK to attempt a connection.")

;execute the default web browser with the levtec web site

ShellExecute("http://www.levtec.co.cc/")

;set what happens when the exit button is clicked

Case $msg = $Button_3

;display a message box

MsgBox(64, "Information", "Thanks for using the levtec celcius-fahrenheit converter." & @CRLF & "Click OK to close.")

;exit the loop and distroy the gui

ExitLoop

;end the select statement

EndSelect

;end the while loop

WEnd

;end the function

EndFunc

;set function to perform conversions

func Conv()

;declare button variables

Local $Button_1, $Button_2, $Button_3, $msg

;create the gui

GUICreate("Convert celcius to fahrenheit or back")

;set the gui buttons

;set the first button to convert celcius to fahrenheit

$Button_1 = GUICtrlCreateButton("Celcius to fahrenheit", 0, -1)

;set the second button to convert fahrenheit to celcius

$Button_2 = GUICtrlCreateButton("Fahrenheit to celcius", 0, -1)

;set the third button to exit the program

$Button_3 = GUICtrlCreateButton("Exit", 0, -1)

;now create the gui

GUISetState()

;Run the GUI until the dialog is closed

While 1

;show the dialog on screen

$msg = GUIGetMsg()

;start a select statement

Select

;set what happens when the app is closed

Case $msg = $GUI_EVENT_CLOSE

;display a message box

MsgBox(64, "Information", "Thanks for using the levtec celcius-fahrenheit converter." & @CRLF & "Click OK to close.")

;exit the loop and distroy the gui

ExitLoop

;set what happens when the celcius to fahrenheit button is clicked

Case $msg = $Button_1

;execute the celcius to fahrenheit function

ConvC()

;set what happens when the fahrenheit to celcius button is clicked

Case $msg = $Button_2

;execute the fahrenheit to celcius function

ConvF()

;set what happens when the exit button is clicked

Case $msg = $Button_3

;display a message box

MsgBox(64, "Information", "Thanks for using the levtec celcius-fahrenheit converter." & @CRLF & "Click OK to close.")

;exit the loop and distroy the gui

ExitLoop

;end the select statement

EndSelect

;end the while loop

WEnd

;end the function

EndFunc

;set function to convert celcius to fahrenheit

Func ConvC()

;set a variable to hold the entered number

Local $C = InputBox("Celcius to fahrenheit converter", "Enter a temperature in celcius to convert to fahrenheit. No decimals allowed.")

;get the length of the number entered

Local $clen = StringLen($C)

;check that the length is not 0

if $clen == "0" then

;display a message box

MsgBox(16, "Error", "There was no number entered." & @CRLF & "Please click OK to return to the main page.")

else

;check that the entry is all numeric

if not StringIsDigit($C) then

;display a message box with an error

MsgBox(16, "Error", "There was non-numerical input detected or you used decimals." & @CRLF & "Decimal places are not allowed." & @CRLF & "Click OK to return to the main page.")

else

;set a variable to hold the converted number

Local $F = $C * 9 / 5 + 32

;display a message box with the entered string and it's hex value

MsgBox(64, "Celcius to fahrenheit converter", "Original temperature in celcius:" & @CRLF & $C & @CRLF & "Resulting temperature in fahrenheit:" & @CRLF & $F & @CRLF & "Click OK to return to the main page.")

;end the if statement

EndIf

;end the if statement

EndIf

;end the function

EndFunc

;set function to convert fahrenheit to celcius

Func ConvF()

;set a variable to hold the entered number

Local $F = InputBox("Fahrenheit to celcius converter", "Enter a temperature in fahrenheit to convert to celcius. No decimals allowed.")

;get the length of the number entered

Local $flen = StringLen($F)

;check that the length is not 0

if $flen == "0" then

;display a message box

MsgBox(16, "Error", "There was no number entered." & @CRLF & "Please click OK to return to the main page.")

else

;check that the entry is all numeric

if not StringIsDigit($F) then

;display a message box with an error

MsgBox(16, "Error", "There was non-numerical input detected or you used decimals." & @CRLF & "Decimal places are not allowed." & @CRLF & "Click OK to return to the main page.")

else

;set a sequence of variables to hold the converted number, the reason for the sequence is the problematic conversion of fahrenheit to celcius

Local $C1 = $F

Local $C2 = $C1 -32

Local $C3 = $C2 *5

Local $C = $c3 /9

;display a message box with the entered string and it's hex value

MsgBox(64, "fahrenheit to celcius converter", "Original temperature in fahrenheit:" & @CRLF & $F & @CRLF & "Resulting temperature in celcius:" & @CRLF & $C & @CRLF & "Click OK to return to the main page.")

;end the if statement

EndIf

;end the if statement

EndIf

;end the function

EndFunc

Please note: If you plan on submitting any code snippits to my posts, please refrain from using the code tags as i use a screen reader and can't read the code properly, let alone copy it the way it should be.Thanks for your understanding.Best regardsXY16

Link to comment
Share on other sites

One or two comments. I think you should allow people the option to vist your website at their leisure, rather than that being the first message. When I click on cancel, I would expect the message box to disappear rather than an Error message being shown. Cancelling an action normally returns a program to an inactive or ready state.

Regarding font size and the width of the buttons on the GUI. I will create a table containing the default width of letters using the standard GUI font. Hopefully you will be able to use it to judge the width of your buttons. At the moment you can read the text okay, but it's slightly bunched up. There should be a bit more space, also between the buttons themselves.

Edited by czardas
Link to comment
Share on other sites

hi

Thanks for your comments and helpful input. also thanks for your kind offer.

i don't really plan on doing anything else to the converter as it was just a little project as one of my first autoit programs. there's nothing really i can do to it that would be a good update apart from the things you said. converters like that don't really need regular updates as currency converters would but i might get round to updating it soon.

Please note: If you plan on submitting any code snippits to my posts, please refrain from using the code tags as i use a screen reader and can't read the code properly, let alone copy it the way it should be.Thanks for your understanding.Best regardsXY16

Link to comment
Share on other sites

IGNORE THIS POST AND SEE POST NUMBER 7 FOR A MUCH EASIER SOLUTION.

I created a label 800 pixels wide. Then I tested each letter of the alphabet, repeating it as many times as I could before running out of space. So to get a good estimate of the width of a letter in pixels, you need to divide 800 by the number of repeats for each letter. Here are the first results.

Measuring the number of times each letter can be repeated in an 800 pixel wide button or label, using the default font size for GUI text.

Results for capital letters follow.

A = 114

B = 114

C = 114

D = 100

E = 114

F = 133

G = 100

H = 100

I = 267

J = 160

K = 114

L = 133

M = 89

N = 100

O = 100

P = 114

Q = 100

R = 100

S = 114

T = 114

U = 100

V = 114

W = 72

X = 114

Y = 114

Z = 114

Results for lower case letters follow.

a = 133

b = 133

c = 133

d = 133

e = 133

f = 266

g = 133

h = 133

i = 399

j = 399

k = 133

l = 399

m = 100

n = 133

o = 133

p = 133

q = 133

r = 266

s = 160

t = 266

u = 133

v = 133

w = 100

x = 160

y = 160

z = 160

Other Characters follow:

Space = 267

All numbers are the same width and can be repeated 133 times in the 800 pixel label.

0 = 133

1 = 133

2 = 133

3 = 133

4 = 133

5 = 133

6 = 133

7 = 133

8 = 133

9 = 133

End of list.

It is easy to see that the widest letter is the capital letter W.

It is not necessary to calculate the exact size of any particular text on a button or label. It is just necessary to leave some extra space to compensate for some letters that are wider than others.

From these figures I calculate that the average width of a letter is 5.41 pixels. So I suggest that you just make sure that there is more than enough space for the text.

You could also use a font that does not vary in width. I know of two, but I'm sure there must be others. One is Lucida Console and the other is Courier New. Lucida Console is a bit ugly so maybe Courier New would be better. I'll check these out later for you. It is also better to have some padding around the word anyway, to make it appear less cluttered.

I hope this is useful to you.

New Update:

Now that I have done this, I have found a much simpler solution. If you use the default text autofit, then you should put one or two spaces before and after the text. That makes the text appear much clearer. The autofit goes exactly to the edge of the word, but with spaces before and after the word, the button looks uncluttered.

SEE POST NUMBER 7 FOR A MUCH EASIER SOLUTION.

Edited by czardas
Link to comment
Share on other sites

In your MainGUI() function you have these three lines in which you're setting $Button_3 two times:

;set the first button to perform conversions

$Button_1 = GUICtrlCreateButton("Convert", 0, -1)

;set the second button to visit the levtec web site

$Button_3 = GUICtrlCreateButton("Web site", 0, -1)

;set the third button to exit the program

$Button_3 = GUICtrlCreateButton("Exit", 0, -1)

Ok, I am a buffoon. I just now learned that you are blind. Sorry.

Edited by jaberwocky6669
Link to comment
Share on other sites

I decided to look in the help file to see if there was a simple way to get the ideal size for a button. I found a function called _GUICtrlButton_GetIdealSize. Look it up in the help file. Also check the example I have written below.

; Start of Script.

#include <GUIConstantsEx.au3>

#include <GuiButton.au3>

; Prompt the user to enter the text for the button.

$_text = InputBox("Button Text", "Enter the text that will appear on the button," & @CRLF & "and then press enter.", "")

; Create the GUI to contain the button.

GUICreate("My Button Test", 800, 100)

; Create the button.

$_button = GUICtrlCreateButton($_text, 10, 10)

; It is not necessary to display the GUI to get the ideal size for the button.

; Get the ideal size for the button.

$a_Ideal_Size = _GUICtrlButton_GetIdealSize($_button)

; Return the ideal width and height for the button.

MsgBox(0, "Ideal size of the button in pixels.", "Width: " & $a_Ideal_Size[0] & @CRLF & "Height: " & $a_Ideal_Size[1])

; End of script.

I have tested it and it works fine. I personally think that you should add an extra 6 pixels to the width, to make the button less cluttered. If you are using several buttons, it is often more attractive to the user if you create them all the same size (although this isn't necessarily required). In this case you need to find the minimum size needed for the buttons. The same code can also be used to get the width required for labels. I also suggest that you leave a small gap between each button or label. Approximately 5 to 10 pixels should be enough to avoid clutter.

To get a good layout will mean making a few calculations, but the best coders do that anyway. It isn't necessary to see the GUI, because everything can be positioned precisely. I am sure that people on the forum will be happy to check the appearance of any GUI you create. Perhaps your first attempts will require a few modifications, but I'm sure that you will get a feel for it before long, when you persevere.

You said that your screen reader has problems reading the Code Boxes on this forum. There is normally a popup option that you can click. If you can locate it, the code will appear in a separate window. Then you can easily copy the code and paste it into your editor. It should require no further modification.

Edited by czardas
Link to comment
Share on other sites

thanks for the feedback. i will try and find time in my busy schedule to fix it.

Please note: If you plan on submitting any code snippits to my posts, please refrain from using the code tags as i use a screen reader and can't read the code properly, let alone copy it the way it should be.Thanks for your understanding.Best regardsXY16

Link to comment
Share on other sites

hi

thanks allott for the example script. that's certainly really usefull and something i missed in the help file

regarding the popup option, i find that i am unable to click the link. because the screen reader uses a separate virtual cursor to navigate, it's also hard for people to know where my mouse is positioned unless i explicitely tell the screen reader to snap the cursor to the virtual cursor position, but that feature also produces a slight bug where the onscreen moddle isn't able to keep up with screen refreshes. i will try to work a fix and have it put in the next beta of hal, but i can't promise anything. in the mean time, if people could continue to just put the code in without tags, i would be greatful.

thanks for all the help. i don't know what i would do without you guys, other forums aren't so helpfull. my main language is c and c++ but i just get arrogance and abuse when i ask for help on forums like code project etc, stuff like

"O my god man, i can't believe you can't even make a gui. lol."

"Wow. it comes to something when someone can't even put a button in the right place"

so i tend not to use such forums.

Please note: If you plan on submitting any code snippits to my posts, please refrain from using the code tags as i use a screen reader and can't read the code properly, let alone copy it the way it should be.Thanks for your understanding.Best regardsXY16

Link to comment
Share on other sites

I also didn't know about that function until now. I thought it might turn out to be useful for you. It's also good find for me too. Thanks for showing this appreciation. It makes me happy.

Regarding those other forums. Although I don't know much about the other forums you mention, I consider people who make such comments to be really quite retarded. There are some people who dwell on the negative, and fail to recognize any kind of acheivement by others whatsoever. I can imagine it being very frustrating having to deal with things like that. There are a lot of very helpful people on this forum, and it's good to have you as part of the AutoIt community.

Cheers ;)

Edited by czardas
Link to comment
Share on other sites

hi czardas

thanks for showing such understanding. my reply to such people if i feel that they need one is usually something like

"yeah, and can you type with your eyes closed, thought not. let alone code and make a gui, or even bring up your ide to get started."

being blind is not all that bad, there is abuse (physically, verbally and mentally) that we have to deal with some times, but it just makes a man stronger in his resolve to succeed and do well in life.

i have a friend who was made a eunuch by a kid for being blind. the kid said people like us should never be allowed to reproduce, let alone be seen in public. we were both at school at the time and i have never managed to shake the memory of that day. this friend went on to adopt 3 kids as he could no longer have any of his own, so made a difference in the world to kids who needed him.

back to coding, i think i may have made a breakthrough in gui design for the blind and am having a sighted professor at my old college who is proficient in autoit help me build a program to try to arrange an already existing gui. the idea behind it is to create your control and set all it's properties to 0 etc for the text and names, then run the program and have it try to calculate positions relative to a set of samples that it will have to work with. i know this will be a real challenge and will take untold thought and time to effect, but i am hopeful that it will work.

once again thanks for all your help.

Please note: If you plan on submitting any code snippits to my posts, please refrain from using the code tags as i use a screen reader and can't read the code properly, let alone copy it the way it should be.Thanks for your understanding.Best regardsXY16

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...