你可以参考一下,用字符串实现
a = g = d = "##########"
b = c = ' ' * 8 + "#"
e = f = "#"
def do(args):
if 'a' in args:
print(a)
if 'f' in args:
if 'b' in args:
print((f + b + '\n') * 3, end='')
else:
print((f + '\n') * 3, end='')
else:
if 'b' in args:
print((' ' + b + '\n') * 3, end='')
else:
print('\n' * 3, end='')
if 'g' in args:
print(g)
if 'e' in args:
if 'c' in args:
print((e + c + '\n') * 3, end='')
else:
print((e + '\n') * 3, end='')
else:
if 'c' in args:
print((' ' + c + '\n') * 3, end='')
else:
print('\n' * 3, end='')
if 'd' in args:
print(d)
if __name__ == '__main__':
do(['a', 'b', 'g', 'e'])
print('\n')
do(['a', 'f', 'e', 'd'])
print('\n')
do(['a', 'b', 'c', 'd', 'e', 'f', 'g'])
def show(string):
if "a" in string:
print(" —— ")
if "f" in string and "b" in string:
print("| |")
elif "f" in string:
print("| ")
else:
print(" |")
if "g" in string:
print(" —— ")
if "e" in string and "c" in string:
print("| |")
elif "e" in string:
print("| ")
else:
print(" |")
if "d" in string:
print(" —— ")
show("abge")
这样可以吗?
# Coding:GB2312
from tkinter import * # 导入图形模块,from=从,import=导入(从tkinter中导入所有的函数),这样可直接使用tkinter中的所有函数
from itertools import *
# 第一步:创建主窗口
win = Tk() # 调用Tk()创建窗口
win.title('火柴') # 设置窗口的标题
win.geometry('400x250') # 设置窗口大小(可省略,但是很容易导致画面崩坏)【字母x】
# 第二步:先定位火柴的位置
# 0列 1列 2列
# 0行 —
# 1行 | |
# 2行 —
# 3行 | |
# 4行 —
# 提示:先定位好火柴的位置,然后通过循环,生成火柴的不同排序方式,再依次点亮火柴。
"""
hc_a = Button(win,text='A',state='active',width=10,height=3)
Button 创建一个按钮,我那它当做火柴;win就是说这个按钮显示在win这个窗口中
text 中填入你想输出的文本内容
state 状态,disabled关闭按钮(关闭后的按钮是灰色)active开启按钮(开启后的按钮正常显示颜色)但并不影响背景颜色
width 宽度,设置按钮的宽度(不设置的话,看起来不协调)
height 高度,设置俺就的高度(不设置的话,看起来不协调)
bg 背景颜色,默认输出的按钮无法显示背景颜色,控制state的变化可以触发背景颜色变化
"""
# bg=背景,设置背景颜色(但需要按钮被点击后才可以触发)
hc_a = Button(win, text='A', bg='red', state='active', width=10)
hc_a.grid(row=0, column=1) # 按照火柴的位置布置按钮(row为行,column为列)
hc_f = Button(win, text='F', bg='red', state='active', height=3, width=3)
hc_f.grid(row=1, column=0)
hc_b = Button(win, text='B', bg='red', state='active', height=3, width=3)
hc_b.grid(row=1, column=2)
hc_g = Button(win, text='G', bg='red', state='active', width=10)
hc_g.grid(row=2, column=1)
hc_e = Button(win, text='E', bg='red', state='active', height=3, width=3)
hc_e.grid(row=3, column=0)
hc_c = Button(win, text='C', bg='red', state='active', height=3, width=3)
hc_c.grid(row=3, column=2)
hc_d = Button(win, text='D', bg='red', state='active', width=10)
hc_d.grid(row=4, column=1)
# 第三步:生成火柴摆放组合【脑子笨,用笨方法】
temp_list = [] # 存放火柴摆放组合
for a1 in range(2): # 0关闭显示,1 显示颜色
for a2 in range(2): # b
for a3 in range(2): # c
for a4 in range(2): # d
for a5 in range(2): # e
for a6 in range(2): # f
for a7 in range(2): # g
temp_list.append(
str(a1) + str(a2) + str(a3) + str(a4) + str(a5) + str(a6) + str(a7)) # 将组合合并存在列表中
# 例如第一个摆放组合:temp_list[0],调用第一个组合;temp_list[0][0]第一个组合中的第一个字符元素
# 第四步:创建火柴发光控制函数
def light_up(): # 打开发光
global cid
light_down() # 默认关闭发光
if temp_list[cid][0] == '1': hc_a['state'] = 'disable'
if temp_list[cid][1] == '1': hc_b['state'] = 'disable'
if temp_list[cid][2] == '1': hc_c['state'] = 'disable'
if temp_list[cid][3] == '1': hc_d['state'] = 'disable'
if temp_list[cid][4] == '1': hc_e['state'] = 'disable'
if temp_list[cid][5] == '1': hc_f['state'] = 'disable'
if temp_list[cid][6] == '1': hc_g['state'] = 'disable'
label_text['text'] = '当前组合:' + temp_list[cid] # 更新组合名称
cid += 1
def light_down(): # 关闭发光
hc_a['state'] = 'active'
hc_b['state'] = 'active'
hc_c['state'] = 'active'
hc_d['state'] = 'active'
hc_e['state'] = 'active'
hc_f['state'] = 'active'
hc_g['state'] = 'active'
cid = 0 # 控制组合ID
Button(win, text='发光', command=light_up).place(x=200, y=100) # place(x,y) 的作用将按钮放在指定的坐标上
# 一些辅助提示
label_text = Label(win, text='ABCDEFG')
label_text.place(x=280, y=60)
label_text = Label(win, text='当前组合:' + temp_list[cid], font=('楷体', 12)) # font调整字体
label_text.place(x=200, y=80)
win.mainloop() # 显示窗口,必须完成了窗口绘制后再显示!
Tkinter中的一个小毛病,鼠标触碰按钮才会变色,要么控制按钮的state属性
要自己控制7个管段,那要麻烦些,只能自己画图了。
你要可视化,那肯定要是基于窗口的应用,而不能是基于控制台的
说什么用字符串模拟一下,那不是自欺欺人吗
你好歹引用个qt,拖几个label过来,让它显示或者隐藏
要么引用个pygame之类的
总之能提供窗体的包