Home >Backend Development >Python Tutorial >How Python builds conversational programs with control variables

How Python builds conversational programs with control variables

青灯夜游
青灯夜游forward
2018-10-20 16:12:172364browse

The content of this article is to introduce How Python uses control variables to build dialogue programs. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Control variables

Variables Symbol Meaning Default value
1 var = tk.BooleanVar() Boolean 0
2 var = tk.StringVar() String control variable Empty string
3 var = tk.IntVar() Integer control variable 0
4 var = tk.DoubleVar() Decimal control variable 0.0

Create control variable

var = StringVar()

Set control variable The value

var.set("set a value.")

Get the value of the control variable

var.get("get a value from the control parameter.")

Control variable usage example

import tkinter as tk

root = tk.Tk()

# 创建一个label标签,在上面存放位图或者文字
labelx = tk.Label(root, bitmap='questhead', compound=tk.LEFT)
var = tk.StringVar()
var.set("This is a dialogue.")
labelx.config(textvariable=var)
labelx.config(bg='cyan')
labelx.config(font=('Times New Roman', 20, 'bold'))
labelx.config(relief=tk.FLAT)
labelx.config(bd=5)

# 点击问题按钮后,反应的函数
def askvar():
    var.set("How are you?")

# 点击回答按钮后,反应的函数
def responsevar():
    var.set("I'm fine. Thank you for your asking.")

# 按钮1创建
button1 = tk.Button(root, text="问题", command=askvar)
button1.config(bg='yellow')
button1.config(font=('楷书', 20, 'italic'))
button1.config(relief=tk.GROOVE)

# 按钮2创建
button2 = tk.Button(root, text="回答", command=responsevar)
button2.config(bg='red')
button2.config(font=('楷书', 20, 'italic'))
button2.config(relief=tk.RIDGE)

# 对创建的组件进行排兵布阵
labelx.pack(side=tk.TOP)
button1.pack(side=tk.LEFT)
button2.pack(side=tk.RIGHT)

root.mainloop()

Initial operating status

How Python builds conversational programs with control variables

Reaction after clicking the question

How Python builds conversational programs with control variables

Reaction after clicking the answer

How Python builds conversational programs with control variables

Summary

Only when the program has variables can we have a heart-beating place. Blue sky is the place we yearn for, but the haze Hazel blocks our view. Today is cloudy again. But I have changed.

The above is the detailed content of How Python builds conversational programs with control variables. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete

Related articles

See more