如何通过单击按钮在 tkinter 中重新运行代码

时间:2021-03-01 18:34:09

标签: python tkinter

我有一些 tkinter 代码:

from tkinter import *
import backend
import random

window=Tk()

list_for_comp = ["rock", "paper", "scissors"]
comp_choice =  random.choice(list_for_comp)
def opposite_of_get_colour():
    colours2 = ['#f0f0ed']
    for colour in colours2:
        while True:
            yield colour

   
list1 = Listbox(height = 7, width = 38)
list1.grid(row = 2, column = 0, rowspan = 6, columnspan = 2)
list1.configure(bg = '#f7ff82')

def rock():
    if comp_choice == list_for_comp[0]:
        list1.insert(END, "You drew with the computer.")
    elif comp_choice == list_for_comp[1]:
        list1.insert(END, "Mission failed, we'll get em next time!")
    elif comp_choice == list_for_comp[2]:
        list1.insert(END, "YOU BEAT THE COMPUTER! NOICE")

def paper():
    if comp_choice == list_for_comp[1]:
        list1.insert(END, "Let's see if you can get then next time. You drew this time. At least you didn't lose!")
    elif comp_choice == list_for_comp[0]:
        list1.insert(END, "MISSION ACCOMPLISHED! YOU BEAT THE COMPUTER")
    elif comp_choice == list_for_comp[2]:
        list1.insert(END, "noo, the computer beat you")

def scissors():
    if comp_choice == list_for_comp[0]:
        for rows in backend.view():
            list1.insert(END, "noo, you lost against the computer")
    elif comp_choice == list_for_comp[1]:
        for rows in backend.view():
            list1.insert(END, "NOICE! YOU BEAT THE COMPUTER")
    elif comp_choice == list_for_comp[2]:
        for rows in backend.view():
            list1.insert(END, "It was a tie!")


def play_again():
    #How do I make my code run once more?
    pass

    
b1 = Button(window, text='Rock', bg = '#5cb0e0', command=rock)
b1.grid(row=2, column=3)

b2 = Button(window, text='Paper', bg = '#e39abb', command=paper)
b2.grid(row=3, column=3)

b3 = Button(window, text='Scissors', bg = '#90e89a', command=scissors)
b3.grid(row=4, column=3)

list1.insert(END, "Do you want to play again?")

b4 = Button(window, text = "I do want to play again". command = play_again)
b5 = Button(window, text = "I don;t want to play again", commmand = window.destroy)
window.mainloop()

有两个问题: 我不确定如何使用我创建的 play_again 函数重新运行我的代码。另外,我只希望 play_again 函数仅在用户对着计算机玩完一轮石头剪刀布时才出现。

最后,让你知道,我的后端脚本是这样的:

import sqlite3

def connect():
    conn = sqlite3.connect("books.db")
    cur = conn.cursor()
    conn.commit()
    conn.close()

def insert(title, author, year, isbn):
    conn = sqlite3.connect("books.db")
    cur = conn.cursor()
    conn.commit()
    conn.close()

def view():
    conn = sqlite3.connect("books.db")
    cur = conn.cursor()
    rows = cur.fetchall()
    conn.commit()
    conn.close()
    return rows

1 个答案:

答案 0 :(得分:1)

也许这就是你的意思: (我添加了一个函数 clear() - 它清除石头剪刀布,在函数 play_again 中它们再次出现)

def rock():
    clear()
    #your code

def paper():
    clear()
    #your code

def scissors():
    clear()
    #your code

def clear():
    b1.grid_forget()
    b2.grid_forget()
    b3.grid_forget()
    b4.grid(row=5, column=3)
    b5.grid(row=6, column=3)


def play_again():
    b1.grid(row=2, column=3)
    b2.grid(row=3, column=3)
    b3.grid(row=4, column=3)
    b4.grid_forget()
    b5.grid_forget()