python - 使用 Tkinter 按钮命令
问题描述
我试图在 tkinter 上创建一个应用程序,但我遇到了一些问题。所以问题出在按钮开始,按下时它必须在屏幕上显示结果,但我不知道我应该在命令行中写什么才能使其工作。我正在尝试类似的东西, command = game(answ)
但这不起作用,因为当你启动程序时它已经显示了结果,所以开始按钮没有意义。我能做些什么?较低的是游戏没有参数的代码,因此它不起作用
import tkinter as tk
from tkinter import filedialog, Text
import os
from tkinter import *
import tkinter as tk
from tkinter import ttk
from tkinter import simpledialog
import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import showinfo
count_w = 0
count_l = 0
a = []
ls = []
ws = []
version = '0.1 PreAlpha'
def asking():
global win, win2, answ, bbok
answ = simpledialog.askinteger("input string", " Choose 1 / 2")
if answ == 1 or answ == 2:
win2 = tk.Toplevel()
win2.wm_title("Great Choice")
bbok = tk.Button(win2, text=f'Selected {answ}', command=goback2)
bbok.pack()
root2.destroy()
else:
win = tk.Toplevel()
win.wm_title("Error")
bok = tk.Button(win, text="Try Again! Insert other value", command=goback)
bok.pack()
def goback():
choiceframe.pack()
win.destroy()
def goback2():
win2.destroy()
bbok.destroy()
def goback3():
root.destroy()
def kekes():
win = tk.Toplevel(root, command=showinfo())
win.wm_title("Error")
def game(answ):
adv = True
global a, ws, ls, count_w, count_l
bullet = random.randint(1, 6)
if answ == 1:
enemy = 2
elif answ == 2:
enemy = 1
choice = answ
answ, en1 = 0, 0
i = 0
cnt = 0
while adv == True:
i = i + 1
cnt = i * 2
if choice == 1:
answ = en1 + 1
en1 = answ + 1
elif choice == 2:
en1 = answ + 1
answ = en1 + 1
if answ == bullet:
print('You lost after ', cnt, " shot")
losing = 'You lost king '
ws.append(losing)
adv = False
count_l += 1
elif en1 == bullet:
print("Enemy lost after ", cnt, " shot")
wining = 'You won king '
ls.append(wining)
adv = False
count_w +=1
print(f'Me {count_w} : {count_l} Enemy')
a.append(cnt)
wl = [ls, ws]
def winer_looser(wl):
global restart, wining_an
for lists in wl:
for text in lists:
for words in text.split():
if words == "won":
wining_an = tk.Toplevel()
wining_an.wm_title("You won")
close_butt2 = tk.Button(wining_an, text=" Close ", command = goback3)
close_butt2.pack()
restart = tk.Button(wining_an, text = "Restart", command = restartgame)
restart.pack(expand = 1)
print("You")
if words == "lost":
losing_an = tk.Toplevel()
losing_an.wm_title("You lost")
close_butt = tk.Button(losing_an, text="Close", command=goback3)
close_butt.pack()
restart = tk.Button(wining_an, text="Restart", command=restartgame)
restart.pack(expand=1)
print("Enemy")
def restartgame():
game(answ)
root2 = tk.Tk()
root2.title(f'choser {version}')
choiceframe = tk.Frame(root2, bg='white')
choiceframe.place(relwidth=0.15, relheight=0.025, relx=0.675, rely=0.75)
choiceframe.pack()
buttonframe = tk.Frame(choiceframe, bg='white')
buttonframe.place(relwidth=0.15, relheight=0.025, relx=0.675, rely=0.675)
buttonframe.pack()
B1 = tk.Button(buttonframe, bg='grey', text='Make your choice ', command=asking)
B1.pack(expand=1)
root2.mainloop()
root = tk.Tk()
root.title(f'Russian Roullete {version}')
canvas = tk.Canvas(root, height=800, width=700, bg="#FF5733")
canvas.pack()
mainframe = tk.Frame(root, bg="grey")
mainframe.place(relwidth=0.3, relheight=0.1, relx=0.6, rely=0.8)
start = tk.Button(mainframe, text=" Start !", padx=60, pady=20, fg="black", bg='grey', command= game(answ))
start.pack(expand=1)
frame_count = tk.Frame(root, bg="grey")
frame_count.place(relwidth=0.16, relheight=0.05, relx=0.675, rely=0.72)
for x in a:
moves = tk.Label(frame_count, text=f'Number of moves {x}', bg='grey')
moves.pack(expand=1)
root.mainloop()
print(f'Me {count_w} : {count_l} Enemy')```
解决方案
当你设置
command = game(answ)
它立即调用game
并传递返回值(将是None
),而不是函数本身。如果必须传递 arg,请使用 lambda 函数,如下所示:
command = lambda:game(answ)
推荐阅读
- javascript - 查看每个调用旁边的 javascript 代码的执行时间
- azure - Azure CDN 终结点中的裸域具有错误的 HTTPS 证书名称
- php - 根据另一个输入值(0-100 之间的数字)将 DIV ID(字母)传递/回显到输入值
- python - 在没有scipy的python中将COO转换为CSR格式
- php - HTML 显示为文本
- python - 如何从列表中获取方括号中的唯一值
- html - 可以
在没有父级的情况下使用 - c#-4.0 - 一种使用 SOLID 原理替换 Switch 的方法
- android - ImageView 不以 ConstraintLayout 为中心
- cmake - 在 CMake 项目中添加 Premake 子项目的简单方法?