2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -24,17 +24,23 @@ Typically, the following is needed:
|
|||
* read and check input from the user
|
||||
* pop up dialogs to query the user for further information
|
||||
|
||||
The task: For a minimal "application", write a program that presents
|
||||
a form with three components to the user:
|
||||
A numeric input field ("Value") and two buttons ("increment" and "random").
|
||||
|
||||
;Task:
|
||||
For a minimal "application", write a program that presents a form with three components to the user:
|
||||
::* a numeric input field ("Value")
|
||||
::* a button ("increment")
|
||||
::* a button ("random")
|
||||
|
||||
|
||||
The field is initialized to zero.
|
||||
|
||||
The user may manually enter a new value into the field,
|
||||
or increment its value with the "increment" button.
|
||||
|
||||
Entering a non-numeric value should be either impossible,
|
||||
or issue an error message.
|
||||
|
||||
Pressing the "random" button presents a confirmation dialog,
|
||||
and resets the field's value to a random value if the answer is "Yes".
|
||||
|
||||
(This task may be regarded as an extension of the task [[Simple windowed application]]).
|
||||
(This task may be regarded as an extension of the task [[Simple windowed application]]).<br><br>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,48 @@
|
|||
#include <windows.h>
|
||||
#include "resource.h"
|
||||
|
||||
BOOL CALLBACK DlgProc( HWND hwnd, UINT msg, WPARAM wPar, LPARAM lPar ) {
|
||||
switch( msg ) {
|
||||
|
||||
case WM_INITDIALOG:
|
||||
srand( GetTickCount() );
|
||||
SetDlgItemInt( hwnd, IDC_INPUT, 0, FALSE );
|
||||
break;
|
||||
|
||||
case WM_COMMAND:
|
||||
switch( LOWORD(wPar) ) {
|
||||
case IDC_INCREMENT: {
|
||||
UINT n = GetDlgItemInt( hwnd, IDC_INPUT, NULL, FALSE );
|
||||
SetDlgItemInt( hwnd, IDC_INPUT, ++n, FALSE );
|
||||
} break;
|
||||
case IDC_RANDOM: {
|
||||
int reply = MessageBox( hwnd,
|
||||
"Do you really want to\nget a random number?",
|
||||
"Random input confirmation", MB_ICONQUESTION|MB_YESNO );
|
||||
if( reply == IDYES )
|
||||
SetDlgItemInt( hwnd, IDC_INPUT, rand(), FALSE );
|
||||
} break;
|
||||
case IDC_QUIT:
|
||||
SendMessage( hwnd, WM_CLOSE, 0, 0 );
|
||||
break;
|
||||
default: ;
|
||||
}
|
||||
break;
|
||||
|
||||
case WM_CLOSE: {
|
||||
int reply = MessageBox( hwnd,
|
||||
"Do you really want to quit?",
|
||||
"Quit confirmation", MB_ICONQUESTION|MB_YESNO );
|
||||
if( reply == IDYES )
|
||||
EndDialog( hwnd, 0 );
|
||||
} break;
|
||||
|
||||
default: ;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPInst, LPSTR cmdLn, int show ) {
|
||||
return DialogBox( hInst, MAKEINTRESOURCE(IDD_DLG), NULL, DlgProc );
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
#define IDD_DLG 101
|
||||
#define IDC_INPUT 1001
|
||||
#define IDC_INCREMENT 1002
|
||||
#define IDC_RANDOM 1003
|
||||
#define IDC_QUIT 1004
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
#include <windows.h>
|
||||
#include "resource.h"
|
||||
|
||||
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
|
||||
IDD_DLG DIALOG 0, 0, 154, 46
|
||||
STYLE DS_3DLOOK | DS_CENTER | DS_MODALFRAME | DS_SHELLFONT | WS_CAPTION |
|
||||
WS_VISIBLE | WS_POPUP | WS_SYSMENU
|
||||
CAPTION "GUI Component Interaction"
|
||||
FONT 12, "Ms Shell Dlg" {
|
||||
EDITTEXT IDC_INPUT, 7, 7, 140, 12, ES_AUTOHSCROLL | ES_NUMBER
|
||||
PUSHBUTTON "Increment", IDC_INCREMENT, 7, 25, 50, 14
|
||||
PUSHBUTTON "Random", IDC_RANDOM, 62, 25, 50, 14
|
||||
PUSHBUTTON "Quit", IDC_QUIT, 117, 25, 30, 14
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
import random, tkMessageBox
|
||||
from Tkinter import *
|
||||
window = Tk()
|
||||
window.geometry("300x50+100+100")
|
||||
options = { "padx":5, "pady":5}
|
||||
s=StringVar()
|
||||
s.set(1)
|
||||
def increase():
|
||||
s.set(int(s.get())+1)
|
||||
def rand():
|
||||
if tkMessageBox.askyesno("Confirmation", "Reset to random value ?"):
|
||||
s.set(random.randrange(0,5000))
|
||||
def update(e):
|
||||
if not e.char.isdigit():
|
||||
tkMessageBox.showerror('Error', 'Invalid input !')
|
||||
return "break"
|
||||
e = Entry(text=s)
|
||||
e.grid(column=0, row=0, **options)
|
||||
e.bind('<Key>', update)
|
||||
b1 = Button(text="Increase", command=increase, **options )
|
||||
b1.grid(column=1, row=0, **options)
|
||||
b2 = Button(text="Random", command=rand, **options)
|
||||
b2.grid(column=2, row=0, **options)
|
||||
mainloop()
|
||||
Loading…
Add table
Add a link
Reference in a new issue