如何重新启动我的脚本?
from Tkinter import *
import Tkinter as tk
import tkMessageBox
import time
import re
import string
from random import randint
print "Hangman v1.7 - by Josh & Paul"
bsrly2 = False
bsrly = False
notlie = True
turns = 8
rec = ''
exp = '^[a-z]+$'
textfile = open('dictionary.txt', 'r')
words = textfile.read().split()
n = randint(0, len(words)-1)
word = words[n]
x = 0
w = list(word)
guess = ''
bs = ''
for letter in word:
if letter in guess:
bs += letter + ' '
else:
bs += '_ '
bs = bs.upper()
def MainProgram():
global ui
global guess
global turns
global rec
global bs
global bsrly
global bsrly2
bs = ''
inp = ui.get().strip()
inp = inp.lower()
ui.delete(0, END)
if bsrly2 == True:
root.quit()
if inp == "":
tkMessageBox.showerror("Incorrect Entry", "Error: Please enter a letter")
elif len(inp) > 1:
tkMessageBox.showerror("Incorrect Entry", "Error: Please enter one letter")
elif inp in guess:
tkMessageBox.showerror("Incorrect Entry", "Error: You have already tried that letter")
elif not re.match(exp, inp):
tkMessageBox.showerror("Incorrect Entry", "Error: Please enter a letter")
else:
if inp not in word:
turns -= 1
if turns == 7:
img.configure(image=image0)
if turns == 6:
img.configure(image=image1)
if turns == 5:
img.configure(image=image2)
if turns == 4:
img.configure(image=image3)
if turns == 3:
img.configure(image=image4)
if turns == 2:
img.configure(image=image5)
if turns == 1:
img.configure(image=image6)
guess += ' ' + inp
if turns == 0:
img.configure(image=image7)
bsrly2 = True
if inp not in word:
upd.configure(text= "Wrong, try again")
rec += ' ' + inp
if inp in word:
upd.configure(text= "Thats correct!")
guess2 = rec.upper()
fb2.configure(text = "Wrong letters:" + guess2)
wait = 0
left = 0
for letter in word:
if letter in guess:
bs += letter + " "
else:
bs += '_ '
left += 1
bs = bs.upper()
if left == 0:
bsrly = True
feedback.configure(text=bs)
bs = ''
if bsrly2 == True:
root
upd.configure(text="You lose, the word was " + word)
check()
def check():
if bsrly == True:
root.destroy()
root2 = Tk()
root2.wm_iconbitmap('hmn.ico')
root2.title("You Win!")
youwin = tk.PhotoImage(master=root2, file="YouWin.gif")
winer = Label(master=root2, image=youwin)
winer.image = youwin
winer.grid(row=0, rowspan=20)
wanaquit = Label(master=root2, text="Play Again?")
wanaquit.grid(row=21)
pbuton = Button(master=root2, text="Yes", command=root2.destroy)
pbuton.grid(row=22)
root2.mainloop()
root = Tk()
root.wm_iconbitmap('hmn.ico')
root.title("Hangman v1.7")
image = tk.PhotoImage(file="image.gif")
image0 = tk.PhotoImage(file="image0.gif")
image1 = tk.PhotoImage(file="image1.gif")
image2 = tk.PhotoImage(file="image2.gif")
image3 = tk.PhotoImage(file="image3.gif")
image4 = tk.PhotoImage(file="image4.gif")
image5 = tk.PhotoImage(file="image5.gif")
image6 = tk.PhotoImage(file="image6.gif")
image7 = tk.PhotoImage(file="image7.gif")
content = tk.Frame(root, bg='black')
namelbl = tk.Label(content, text="Enter a letter:", bg="black", fg="green")
feedback = tk.Label(content, text=bs, bg="black", fg="green")
rb = tk.Checkbutton(content, text="Music", bg="black", fg="green")
slave = tk.Label(content, text="", bg="black", fg="green")
slave2 = tk.Label(content, text="", bg="black", fg="green")
upd = tk.Label(content, text="", bg="black", fg="green")
fb2 = tk.Label(content, text="Used letters:", bg="black", fg="green")
ui = tk.Entry(content)
ui["width"] = 2
img = tk.Label(master=content, image=image, bg="black")
ok = tk.Button(content, text="Okay", bg="black", fg="green", command=MainProgram)
ui.focus()
ui.bind('<Return>', (lambda e: MainProgram()))
content.grid(column=0, row=0)
img.grid(column=0, row=0, columnspan=4)
feedback.grid(column=0, row=1)
fb2.grid(column=0, row=2)
slave.grid(row=3)
slave2.grid(row=5)
upd.grid(row=4, columnspan=4)
namelbl.grid(column=0, row=6)
ui.grid(column=1, row=6, sticky=W)
ok.grid(column=1, row=6)
rb.grid(row=7)
root.mainloop()
大家好,我有一个学校作业即将到期,那就是用 GUI 制作一个 Hangman 程序。
一切都运行顺利,除了我不明白当他们在“你赢了”窗口中单击“是”时如何使脚本重新启动?
from Tkinter import *
import Tkinter as tk
import tkMessageBox
import time
import re
import string
from random import randint
print "Hangman v1.7 - by Josh & Paul"
bsrly2 = False
bsrly = False
notlie = True
turns = 8
rec = ''
exp = '^[a-z]+
Hey Everyone, I have a school assignment due soon that is to make a Hangman program with a GUI.
Everything is running smoothly except i dont understand how I can make the script restart when they click Yes in the You Win window?
textfile = open('dictionary.txt', 'r')
words = textfile.read().split()
n = randint(0, len(words)-1)
word = words[n]
x = 0
w = list(word)
guess = ''
bs = ''
for letter in word:
if letter in guess:
bs += letter + ' '
else:
bs += '_ '
bs = bs.upper()
def MainProgram():
global ui
global guess
global turns
global rec
global bs
global bsrly
global bsrly2
bs = ''
inp = ui.get().strip()
inp = inp.lower()
ui.delete(0, END)
if bsrly2 == True:
root.quit()
if inp == "":
tkMessageBox.showerror("Incorrect Entry", "Error: Please enter a letter")
elif len(inp) > 1:
tkMessageBox.showerror("Incorrect Entry", "Error: Please enter one letter")
elif inp in guess:
tkMessageBox.showerror("Incorrect Entry", "Error: You have already tried that letter")
elif not re.match(exp, inp):
tkMessageBox.showerror("Incorrect Entry", "Error: Please enter a letter")
else:
if inp not in word:
turns -= 1
if turns == 7:
img.configure(image=image0)
if turns == 6:
img.configure(image=image1)
if turns == 5:
img.configure(image=image2)
if turns == 4:
img.configure(image=image3)
if turns == 3:
img.configure(image=image4)
if turns == 2:
img.configure(image=image5)
if turns == 1:
img.configure(image=image6)
guess += ' ' + inp
if turns == 0:
img.configure(image=image7)
bsrly2 = True
if inp not in word:
upd.configure(text= "Wrong, try again")
rec += ' ' + inp
if inp in word:
upd.configure(text= "Thats correct!")
guess2 = rec.upper()
fb2.configure(text = "Wrong letters:" + guess2)
wait = 0
left = 0
for letter in word:
if letter in guess:
bs += letter + " "
else:
bs += '_ '
left += 1
bs = bs.upper()
if left == 0:
bsrly = True
feedback.configure(text=bs)
bs = ''
if bsrly2 == True:
root
upd.configure(text="You lose, the word was " + word)
check()
def check():
if bsrly == True:
root.destroy()
root2 = Tk()
root2.wm_iconbitmap('hmn.ico')
root2.title("You Win!")
youwin = tk.PhotoImage(master=root2, file="YouWin.gif")
winer = Label(master=root2, image=youwin)
winer.image = youwin
winer.grid(row=0, rowspan=20)
wanaquit = Label(master=root2, text="Play Again?")
wanaquit.grid(row=21)
pbuton = Button(master=root2, text="Yes", command=root2.destroy)
pbuton.grid(row=22)
root2.mainloop()
root = Tk()
root.wm_iconbitmap('hmn.ico')
root.title("Hangman v1.7")
image = tk.PhotoImage(file="image.gif")
image0 = tk.PhotoImage(file="image0.gif")
image1 = tk.PhotoImage(file="image1.gif")
image2 = tk.PhotoImage(file="image2.gif")
image3 = tk.PhotoImage(file="image3.gif")
image4 = tk.PhotoImage(file="image4.gif")
image5 = tk.PhotoImage(file="image5.gif")
image6 = tk.PhotoImage(file="image6.gif")
image7 = tk.PhotoImage(file="image7.gif")
content = tk.Frame(root, bg='black')
namelbl = tk.Label(content, text="Enter a letter:", bg="black", fg="green")
feedback = tk.Label(content, text=bs, bg="black", fg="green")
rb = tk.Checkbutton(content, text="Music", bg="black", fg="green")
slave = tk.Label(content, text="", bg="black", fg="green")
slave2 = tk.Label(content, text="", bg="black", fg="green")
upd = tk.Label(content, text="", bg="black", fg="green")
fb2 = tk.Label(content, text="Used letters:", bg="black", fg="green")
ui = tk.Entry(content)
ui["width"] = 2
img = tk.Label(master=content, image=image, bg="black")
ok = tk.Button(content, text="Okay", bg="black", fg="green", command=MainProgram)
ui.focus()
ui.bind('<Return>', (lambda e: MainProgram()))
content.grid(column=0, row=0)
img.grid(column=0, row=0, columnspan=4)
feedback.grid(column=0, row=1)
fb2.grid(column=0, row=2)
slave.grid(row=3)
slave2.grid(row=5)
upd.grid(row=4, columnspan=4)
namelbl.grid(column=0, row=6)
ui.grid(column=1, row=6, sticky=W)
ok.grid(column=1, row=6)
rb.grid(row=7)
root.mainloop()
Hey Everyone, I have a school assignment due soon that is to make a Hangman program with a GUI.
Everything is running smoothly except i dont understand how I can make the script restart when they click Yes in the You Win window?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将程序逻辑放入模块顶层,将其放入函数中。完成此操作后,您只需再次调用您的函数即可。根据您现在拥有的代码结构,不可能重新启动您的程序(好吧,至少在不编写另一个程序的情况下无法重新启动)。
编辑:
示例。将此代码封装在一个函数中:
至于在函数之间传递变量,如果您没有太多时间,可以坚持使用全局变量,但最好将所有结果函数放入一个类中并使用实例变量。
textfile = open('dictionary.txt', 'r') words = textfile.read().split() n = randint(0, len(words)-1) word = words[n] x = 0 w = list(word) guess = '' bs = '' for letter in word: if letter in guess: bs += letter + ' ' else: bs += '_ ' bs = bs.upper()上面的代码将导致函数:
至于在函数之间传递变量,如果您没有太多时间,可以坚持使用全局变量,但最好将所有结果函数放入一个类中并使用实例变量。
Insted of putting program logic into module top-level put it into functions. Once you've done that, you just call again your function. With the code structure you have now it is impossible to restart your program (well, at least not without writing another one).
Edit:
Example. Encapsulate this code in a function:
As for passing variables between your functions you can stick with global variables if you do not have much time left, but it would be best to put all of resulting functions into a class and use instance variables.
textfile = open('dictionary.txt', 'r') words = textfile.read().split() n = randint(0, len(words)-1) word = words[n] x = 0 w = list(word) guess = '' bs = '' for letter in word: if letter in guess: bs += letter + ' ' else: bs += '_ ' bs = bs.upper()Code above would result in function:
As for passing variables between your functions you can stick with global variables if you do not have much time left, but it would be best to put all of resulting functions into a class and use instance variables.