I have some tkinter code:
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()
There are two problems: That I am not sure how to rerun my code using the play_again
function that I made. Also, I only want the play_again
function to appear only when the user has played one round of rock paper scissors against the computer.
Finally, just so that you know, my backend script is this:
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
Maybe this is what you meant: (i added a function clear() - it clears the rock paper scissors and in the function play_again they appear 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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.