Python 带Tkinter按钮命令的hel

Python 带Tkinter按钮命令的hel,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我试图在tkinter上创建一个应用程序,但我遇到了一些问题。所以问题在于启动按钮,当按下时,它必须在屏幕上显示结果,但我不知道应该在命令行中写入什么才能使其工作。我尝试了类似于command=game(answ)的方法,但这不起作用,因为当你启动程序时,它已经显示了结果,所以启动按钮没有意义。我能做什么?下面是游戏没有参数的代码,所以它不起作用 import tkinter as tk from tkinter import filedialog, Text import os from tk

我试图在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
),而不是函数本身。如果必须传递参数,请使用lambda函数,如下所示:

command=lambda:game(answ)

谢谢你帮助我。现在我有另一个问题:)我在函数中创建了一个标签,现在当我打包它时(我的程序有一个选项可以运行无限时间(它可以重新启动)),它会显示上一次运行的结果,我能以某种方式修复它吗?