- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
from tkinter import *
import random
class Application(Frame):
"""A Gui Application for a game."""
def __init__(self,master):
"""Initialize the frame."""
super(Application, self).__init__(master)
self.grid()
self.create_widgets()
self.wins = 0
self.losses = 0
self.ties = 0
def computerRandom(self):
options = ["Rock", "Paper", "Scissors", "Lizard", "Spock"]
randomChoice = random.randint(0,4)
def comparison(self, selfComputer):
if self.playerChoice.get()== selfComputer:
return "Draw"
elif self.playerChoice.get() == "Rock" and selfComputer == "Paper":
return "Computer Wins"
elif self.playerChoice.get()== "Paper" and selfComputer == "Scissors":
return "Computer Wins"
elif self.playerChoice.get()== "Scissors" and selfComputer == "Rock":
return "Computer Wins"
elif self.playerChoice.get()== "Lizard" and selfComputer == "Rock":
return "Computer Wins"
elif self.playerChoice.get()== "Lizard" and selfComputer == "Scissors":
return "Computer Wins"
elif self.playerChoice.get()== "Paper" and selfComputer == "Lizard":
return "Computer Wins"
elif self.playerChoice.get()== "Scissors" and selfComputer == "Spock":
return "Computer Wins"
elif self.playerChoice.get()== "Spock" and selfComputer == "Lizard":
return "Computer Wins"
elif self.playerChoice.get()== "Spock" and selfComputer == "Paper":
return "Computer Wins"
elif self.playerChoice.get()== "Rock" and selfComputer == "Spock":
return "Computer Wins"
else:
return "Human Wins"
def play(self):
selfComputer = self.computerRandom()
result = self.comparison(selfComputer)
if result == "Draw":
self.results_txt ="Its a draw"
elif result == "Computer Wins":
self.results_txt = "Unlucky you lost!"
else:
self.results_txt ="Well done you won!"
def create_widgets(self):
"""Create labels and buttons."""
#create welcome label
Label(self,text = "Welcome to the Rock, Paper, Scissors, Spock and Lizard Game\n"
"You chose one of the weapons to fight with and let's see if the computer\n"
"beats you or not!! Enjoy!!").grid(row = 0, column = 0, columnspan=3, sticky = W)
#create difficulty label
Label(self,text = "Which difficulty level would you like to play?:").grid(row = 1, column = 0, columnspan = 3, sticky = W)
#create variables
self.difficulty = StringVar()
self.difficulty.set(None)
self.playerChoice = StringVar()
self.playerChoice.set(None)
self.results = StringVar()
self.computer_lbl = BooleanVar()
self.computer_lbl.set(None)
#radio buttons for difficulty levels
Radiobutton(self,text = "Easy",variable = self.difficulty,value = "easy").grid(row = 2, column = 0, sticky = W)
Radiobutton(self,text = "Hard",variable = self.difficulty,value = "hard").grid(row = 2, column = 3, sticky = E)
Label(self,text = " ").grid(row = 3, column = 0, sticky = W)
#computer
self.computer_lbl = Label(self, text = "Computer")
self.computer_lbl.grid(row = 4, column = 0, sticky = W)
self.computer_lbl = Label(self,text = "[]Rock").grid(row = 5, column = 0, sticky = W)
self.computer_lbl = Label(self,text = "[]Paper").grid(row = 6, column = 0, sticky = W)
self.computer_lbl = Label(self,text = "[]Scissors").grid(row = 7, column = 0, sticky = W)
self.computer_lbl = Label(self,text = "[]Lizard").grid(row = 8, column = 0, sticky = W)
self.computer_lbl = Label(self,text = "[]Spock").grid(row = 9, column = 0, sticky = W)
#player
self.player_lbl = Label(self, text = "Player")
self.player_lbl.grid(row = 4, column = 3, sticky = W)
Radiobutton(self, text ="Rock",variable = self.playerChoice, value = "Rock").grid(row=5, column=3, sticky=W)
Radiobutton(self, text ="Paper",variable = self.playerChoice, value = "Paper").grid(row=6, column=3, sticky=W)
Radiobutton(self, text ="Scissors",variable = self.playerChoice, value = "Scissors").grid(row=7, column=3, sticky=W)
Radiobutton(self, text ="Lizard",variable = self.playerChoice, value = "Lizard").grid(row=8, column=3, sticky=W)
Radiobutton(self, text ="Spock",variable = self.playerChoice, value = "Spock").grid(row=9, column=3, sticky=W)
#buttons
Label(self,text = " ").grid(row = 10, column = 0, sticky = W)
self.bttn2 = Button(self, text = "Fight!", command = self.play)
self.bttn2.grid(row=11, column=3, sticky=W)
self.play_bttn = Button(self,text = "Play!")
self.play_bttn.grid(row=12, column=3, sticky=W)
self.bttn3 = Button(self, text = "Exit", command = root.destroy)
self.bttn3.grid(row=13,column=3, sticky=W)
#Wins,Losses,Ties, and Results
Label(self, text = "Results:").grid(row=5, column=1, sticky=W)
self.results_txt = Text(self, width=30, height=5, wrap=WORD)
self.results_txt.grid(row=5, rowspan=5, column=1, columnspan=1)
self.wins_lbl = Label(self, text = "Wins:")
self.wins_lbl.grid(row=11, column=0, sticky=W)
self.losses_lbl = Label(self, text = "Losses:")
self.losses_lbl.grid(row=12, column=0, sticky=W)
self.ties_lbl = Label(self, text = "Ties:")
self.ties_lbl.grid(row=13, column=0, sticky=W)
#main
root = Tk()
root.title("Rock, Paper, Scissors, Lizard, and Spock ")
root.geometry("465x410")
app = Application(root)
root.mainloop()
我需要它来记分和打球。 Easy 只需要启用剪刀石头布,而 Hard 则需要全部启用。然后在文本框中显示结果。我一直在一遍又一遍地尝试让它工作,但我是 Python 的新手,需要一些指导。谁能帮帮我?
最佳答案
使用 self.playerChoice = StringVar()
使其成为一个属性。
self.results_txt("Well done you won!")
是一个字符串,因此它不可调用。
使用 self.results_txt ="干得好,你赢了!"
需要将computerRandom()
引用为self.computerRandom()
,它是类的一个方法
self.computer.set(options[randomChoice])
将不起作用,因为您的类中没有 computer
属性。
使用 comparison(self, selfComputer)
会是更好的做法。
此外,result = comparison(self, selfComputer)
将再次不起作用。使用self.comparison
,也是一种方法。
此代码应该更接近您的需要,我不确定 self.computer.set(options[randomChoice])
应该做什么,必须修复。
from tkinter import *
import random
class Application(Frame):
"""A Gui Application for a game."""
def __init__(self,master):
"""Initialize the frame."""
super().__init__()
self.grid()
self.create_widgets()
self.wins = 0
self.losses = 0
self.ties = 0
def computerRandom(self):
options = ["Rock", "Paper", "Scissors", "Lizard", "Spock"]
randomChoice = random.randint(0,4)
self.computer.set(options[randomChoice])
return options[randomChoice]
def comparison(self, selfComputer): # use self as the first parameter, it refers to each instance
# use if/elif, eilf's will only be evaluated if the previous statement is False
if self.playerChoice.get()== selfComputer:
return "Draw"
elif self.playerChoice.get() == "Rock" and selfComputer == "Paper":
return "Computer Wins"
elif self.playerChoice.get()== "Paper" and selfComputer == "Scissors":
return "Computer Wins"
elif self.playerChoice.get()== "Scissors" and selfComputer == "Rock":
return "Computer Wins"
elif self.playerChoice.get()== "Lizard" and selfComputer == "Rock":
return "Computer Wins"
elif self.playerChoice.get()== "Lizard" and selfComputer == "Scissors":
return "Computer Wins"
elif self.playerChoice.get()== "Paper" and selfComputer == "Lizard":
return "Computer Wins"
elif self.playerChoice.get()== "Scissors" and selfComputer == "Spock":
return "Computer Wins"
elif self.playerChoice.get()== "Spock" and selfComputer == "Lizard":
return "Computer Wins"
elif self.playerChoice.get()== "Spock" and selfComputer == "Paper":
return "Computer Wins"
elif self.playerChoice.get()== "Rock" and selfComputer == "Spock":
return "Computer Wins"
else:
return "Human Wins"
def play(self):
selfComputer = self.computerRandom() # again we need to call a method using self
result = self.comparison(selfComputer) # comparing self to `selfComputer`
if result == "Draw":
self.results_txt ="Its a draw"
elif result == "Computer Wins":
self.results_txt = "Unlucky you lost!"
else:
self.results_txt ="Well done you won!"
def create_widgets(self):
"""Create labels and buttons."""
#create welcome label
Label(self,text = "Welcome to the Rock, Paper, Scissors, Spock and Lizard Game\n"
"You chose one of the weapons to fight with and let's see if the computer\n"
"beats you or not!! Enjoy!!").grid(row = 0, column = 0, columnspan=3, sticky = W)
#create difficulty label
Label(self,text = "Which difficulty level would you like to play?:").grid(row = 1, column = 0, columnspan = 3, sticky = W)
#create variables
self.playerChoice = StringVar()
self.playerChoice.set(None)
self.results = StringVar()
关于python - gui python 中的岩石、布、剪刀、蜥蜴、spock 游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25311261/
我有一个无 GUI 的服务器(没有任何桌面环境或 Ubuntu 服务器的新鲜 Debian,没有 X 服务器,先验)。 我考虑安装 docker 并拉取一个基于官方 Ubuntu 的容器,并在其上添加
我正在构建一个带有临时用户名系统的简单聊天服务器。当屏幕弹出时,首先会出现一个简单的屏幕,询问您的用户名。你可以放入任何你想要的东西,这纯粹是暂时的(我也在尝试)。代码告诉程序继续,将用户名保存到代码
我想将来自其他类的图像显示到 QLabel 中,但要通知 GUI 有一个新的框架可用。我需要从非 GUI 类和非 GUI 线程发出信号。 有什么办法吗? 最佳答案 signal 可以从任何继承QObj
我正在用 Java 编写一个图形用户界面,它有一些按钮,其中一个按钮是选项。我想要它,所以当您单击选项时,它会将 gui 更改为我的选项 gui,而不是在另一个窗口中打开它。 我该怎么做? 最佳答案
标题说明了一切...我和我的 friend 正在这样做,我们不知道为什么 Ball.java 实际上没有在 gamePanel 中制作球,然后制作 GUI。顺便说一句,这是 8 球台球。这是代码: 驱
我正在使用 GUI 构建器,我想知道是否有一种简单的方法可以通过当前主窗口打开寄存器窗口(引用下面的页面)。我正在尝试通过菜单栏来执行此操作。 我一整天都在尝试,因为 GUI Builder 生成了一
我有一个程序使用了许多隐藏的 GUI 组件。例如,所有菜单项和打印机对话框/组件(仅占用至少 50 毫秒)。总的来说,我猜整个程序启动的大约 300 毫秒(或 40%)要归功于所有隐藏的东西。 我想做
我对 GUI 构建比较陌生。 我想制作一个带有按钮(我已经有了)的 GUI,用户可以按下该按钮并选择一个图像,然后动态地将该图像加载到面板中的 GUI 中。我希望每次用户浏览图像时图像都动态变化。 到
我有两年使用 Java 和 Visual Studio 进行企业应用程序编程的经验,而且我是 Python 和 wxPython 的新手。所以我的问题是:wxPython 能否为我提供足够丰富的 GU
这是我启动 mkvtoolnix-gui 时遇到的错误: mkvtoolnix-gui: symbol lookup error: mkvtoolnix-gui: undefined symbol:
我在初始屏幕上有一些最近使用的存储库,我想删除它们,因为我不再使用它们了。如何删除它们? 操作系统 = Windows 7 我查看了注册表并搜索了 git 目录,但找不到最近使用列表的存储位置。 最佳
我正在尝试在 matlab、GUI 中用户输入点作为输入和它们之间的连接。 我有 5 个 matlab 文件 - screen1.m、screen2.m、screen3.m、screen4.m、glo
我用java制作了一个客户端/服务器程序,我已经按照我想要的方式使用cmd完美地工作了,现在我正在尝试将代码的客户端转换为GUI,但是我在打印时遇到问题客户端消息并从文本字段和服务器消息读取客户端输入
我正在制作一种 CRUD 应用程序(Java GUI,MYSQL)我应该: 将数据从数据库加载到List(例如),然后将List加载到GUI 将数据从数据库加载到对象(具有 SQL 表等属性)和对象到
我正在开发一个有 5 个图形用户界面窗口的 Java 应用程序,其中一个是问候窗口或主窗口,我已经完成了所有逻辑部分的工作,我已经完成了 99.99%,唯一剩下的就是我如何以这种方式编码,当我点击一个
我目前正在开发 GUI。 我选择将我的 GUI 基于 bluej 项目 - Scribble。 当您创建 ScribbleGUI 对象时,DrawDemo 类会创建一个同时自动打开的 Canvas 。
在这里阅读了很多关于多进程、管道等的内容后,我还没有找到答案,但如果它已经存在,我深表歉意。 我有一个外围硬件,我正在尝试为其创建一个 GUI。我想让 GUI 使用来自外围设备的数据不断更新,同时仍保
我想做的是将 GUI 从一个单独文件中的类链接到另一个类。我的第一个类是一个主菜单,它将显示一些链接到另一个窗口的按钮。第二个类显示不同的窗口,但我现在遇到的问题是我不知道如何链接第一个类中的按钮来调
我的 GUI 代码中有一个奇怪的行为。如果用户在短时间内产生大量事件,则可能会发生正在运行的事件处理程序方法被另一个事件处理程序方法中断。由于一切都在同一个线程(GUI 线程)中运行,所以一切都应该按
这是一个涉及风格的问题。我正在寻找可以帮助我解决常见 GUI 设计问题 的想法。该应用程序是在 Winforms 中完成的,宁愿使用 WPF,该应用程序已经完成,但我是一个完美主义者,在与其他人合作时
我是一名优秀的程序员,十分优秀!