代码如下:
from tkinter import *
import itertools
import threading
import tkinter as tk
root = tk.Tk()
root.title('24点')
root.geometry("600x400")
tk.Label(root,text='请输入您的数字1:').place(x=120,y=1)
tk.Label(root,text='请输入您的数字2:').place(x=120,y=22)
tk.Label(root,text='请输入您的数字3:').place(x=120,y=42)
tk.Label(root,text='请输入您的数字4:').place(x=120,y=62)
b1 = tk.IntVar()
b2 = tk.IntVar()
b3 = tk.IntVar()
b4 = tk.IntVar()
p1 = tk.Entry(root,textvariable=b1)
p1.pack()
p2 = tk.Entry(root,textvariable=b2)
p2.pack()
p3 = tk.Entry(root,textvariable=b3)
p3.pack()
p4 = tk.Entry(root,textvariable=b4)
p4.pack()
c1 = b1.get()
c2 = b2.get()
c3 = b3.get()
c4 = b4.get()
def thread_it(func, *args):
t = threading.Thread(target=func, args=args)
t.setDaemon(True)
t.start()
def do():
while True:
try:
n1 = int(c1)
n2 = int(c3)
n3 = int(c3)
n4 = int(c4)
pokers = (n1, n2, n3, n4)
ans = set()
count = 0
for nums in itertools.permutations(pokers):
for oper in itertools.product("+-*/", repeat=3):
mode1 = " (({0} {4} {1}) {5} {2} ) {6} {3} ".format(*nums, *oper);
mode2 = " ({0} {4} ({1} {5} {2})) {6} {3} ".format(*nums, *oper);
mode3 = " {0} {4} ({1} {5} ({2} {6} {3}))".format(*nums, *oper);
mode4 = " {0} {4} (({1} {5} {2} ) {6} {3})".format(*nums, *oper);
mode5 = " ({0} {4} {1}) {5} ({2} {6} {3})".format(*nums, *oper);
for express in [mode1, mode2, mode3, mode4, mode5]:
ans.add(express)
for oper in ans:
try:
if abs(eval(oper) - 24) <= 0.0001:
count = count + 1
print(" 第%04d的解法 %s=24" % (count, oper))
except:
continue
except EOFError:
break
button = Button(root,text='计算',height=3,width=15,command=lambda :thread_it(do))
button.pack()
root.mainloop()
改为这样按按钮没有反应
import itertools
import tkinter as tk
from tkinter import *
root = tk.Tk()
root.title('24点')
root.geometry("600x400")
tk.Label(root,text='请输入您的数字1:').place(x=120,y=1)
tk.Label(root,text='请输入您的数字2:').place(x=120,y=22)
tk.Label(root,text='请输入您的数字3:').place(x=120,y=42)
tk.Label(root,text='请输入您的数字4:').place(x=120,y=62)
b1 = tk.StringVar()
b2 = tk.StringVar()
b3 = tk.StringVar()
b4 = tk.StringVar()
p1 = tk.Entry(root,textvariable=b1)
p1.pack()
p2 = tk.Entry(root,textvariable=b2)
p2.pack()
p3 = tk.Entry(root,textvariable=b3)
p3.pack()
p4 = tk.Entry(root,textvariable=b4)
p4.pack()
c1 = b1.get()
c2 = b2.get()
c3 = b3.get()
c4 = b4.get()
def do():
try:
pokers = (c1, c2, c3, c4)
ans = set()
count = 0
for nums in itertools.permutations(pokers):
for oper in itertools.product("+-*/", repeat=3):
mode1 = " (({0} {4} {1}) {5} {2} ) {6} {3} ".format(*nums, *oper);
mode2 = " ({0} {4} ({1} {5} {2})) {6} {3} ".format(*nums, *oper);
mode3 = " {0} {4} ({1} {5} ({2} {6} {3}))".format(*nums, *oper);
mode4 = " {0} {4} (({1} {5} {2} ) {6} {3})".format(*nums, *oper);
mode5 = " ({0} {4} {1}) {5} ({2} {6} {3})".format(*nums, *oper);
for express in [mode1, mode2, mode3, mode4, mode5]:
ans.add(express)
for oper in ans:
try:
if abs(eval(oper) - 24) <= 0.0001:
count = count + 1
print(" 第%04d的解法 %s=24" % (count, oper))
except:
continue
except EOFError:
return
button = Button(root,text='计算',height=3,width=15,command=do)
button.pack()
root.mainloop()
threading和whileTrue这样的语句不建议在tkinter里面使用,因为很容易卡死了,并且还无法发现出现了什么问题。还有就是Entry里面即便你输入了数字,程序也应该会认为你输入的是字符串,所以应该把上面的IntVar改成StringVar
哈哈,我也写了一个24点游戏,不过我是用的pyqt5,已经做完了,个人觉得还不错,有兴趣的话,可以交流一下。
24pointgame.zip-其他文档类资源-CSDN下载 24点游戏可执行文件,源代码私聊更多下载资源、学习资料请访问CSDN下载频道. https://download.csdn.net/download/weixin_47411110/21462022
可以试试