Python基础语法(4)——GUI编程及猜数字游戏

13 图形界面(GUI)及猜数字游戏
1 GUI:Graphical User Interface
2 tkinter:GUI library for Python
3 GUI Example
13.1 图形界面
from tkinter import *
import tkinter.messagebox as mb
import tkinter.simpledialog as dl
root=Tk()
w=Label(root,text= "Label" )
w.pack()
mb.showinfo( "welcome" , "welcome learn Python!" )
guess=dl.askinteger( "Number" , "please input an number:" )
mb.showinfo( "output:" ,guess)

13.2 猜数字游戏
from tkinter import *
import tkinter.messagebox as mb
import tkinter.simpledialog as dl
root=Tk()
number= 66
guessFlag= False
while guessFlag== False :
    guess=dl.askinteger( "Number Game" , "please input the number:" )
    if guess==number:
        mb.showinfo( "Congratulation" , "You win!" )
        guessFlag= True
    elif guess>number:
        mb.showinfo( "Sorry" , "The number you input is bigger than true number!" )
    else :
        mb.showinfo( "Sorry" , "The number you input is smaller than true number!" )
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#编写的GUI游戏 源代码 using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace gn { public partial class Form1 : Form { string ready; int counti = 1; public Form1() { InitializeComponent(); this.label1.Hide(); this.button2.Hide(); this.button1.Hide(); this.button3.Hide(); this.button4.Hide(); this.textBox1.Hide(); this.pictureBox1.Hide(); this.pictureBox2.Hide(); } private void button1_Click(object sender, EventArgs e) { string number; int x, y; int count = 0; number = textBox1.Text; if (number.Length != 4) { MessageBox.Show("请输入4位数字"); } else { if (counti <=8) { if (count <= 3) { x = 0; y = 0; for (int i = 0; i <= 3; i++) { for (int j = 0; j <= 3; j++) { if (number[i] == ready[j]) { if (i == j) ++x; else ++y; } } } label2.Text = "你已经猜过" + counti + "次"; ++count; label3.Text = x.ToString() + "A" + y.ToString() + "B"; if (number == ready) { this.label2.Text = "答对了"; this.pictureBox1.Show(); } } if (counti == 8) { this.button1.Enabled = false; this.pictureBox2.Show(); } } else { this.button1.Enabled = false; this.pictureBox2.Show(); } ++counti; } } private void button2_Click(object sender, EventArgs e) { MessageBox.Show(ready); } private void button3_Click(object sender, EventArgs e) { //this.button2.Hide(); this.textBox1.Text = ""; this.label3.Text = ""; this.label2.Text = ""; this.button1.Enabled = true; counti = 1; this.pictureBox1.Hide(); this.pictureBox2.Hide(); Random r = new Random(); string[] a; a = new string[8]; a[0] = "1263"; a[1] = "6598"; a[2] = "9654"; a[3] = "5986"; a[4] = "2915"; a[5] = "2046"; a[6] = "9035"; a[7] = "1057"; int z; z = r.Next(8); ready = a[z]; } private void label1_Click(object sender, EventArgs e) { } private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { } private void button4_Click(object sender, EventArgs e) { Close(); } private void button5_Click(object sender, EventArgs e) { Random r = new Random(); string[] a; a = new string[8]; a[0] = "1263"; a[1] = "6598"; a[2] = "9654"; a[3] = "5986"; a[4] = "2915"; a[5] = "2046"; a[6] = "9035"; a[7] = "1057"; int z; z = r.Next(8); ready = a[z]; this.textBox1.Show(); this.button1.Show(); this.button3.Show(); this.button4.Show(); this.label1.Show(); this.button2.Show(); this.button5.Hide(); } private void label4_Click(object sender, EventArgs e) { } private void Form1_Load(object sender, EventArgs e) { } private void pictureBox1_Click(object sender, EventArgs e) { } private void pictureBox1_Click_1(object sender, EventArgs e) { } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值