🕵️ SicherheitslückenHak5: Hackers Just Poisoned the Rust Supply Chain | Threat Wire(01.09.2026 um 14:00 Uhr)
🕵️ SicherheitslückenHak5: Hackers Found a Way Into Humanoid Robots | Threat Wire(04.09.2026 um 15:04 Uhr)
🔧 AI Nachrichten Bits und so #1021 (Passwort für Laufwerk)(31.08.2026 um 22:15 Uhr)
🔧 AI Nachrichten Bits und so #1022 (Wie Weißbier)(06.09.2026 um 20:39 Uhr)
🍏 iOS / Mac OSHue-App 6.0 ist da: das sind die Neuerungen(07.09.2026 um 17:21 Uhr)
🕵️ SicherheitslückenHak5: Hackers Just Poisoned the Rust Supply Chain | Threat Wire(01.09.2026 um 14:00 Uhr)
🕵️ SicherheitslückenHak5: Hackers Found a Way Into Humanoid Robots | Threat Wire(04.09.2026 um 15:04 Uhr)
🔧 AI Nachrichten Bits und so #1021 (Passwort für Laufwerk)(31.08.2026 um 22:15 Uhr)
🔧 AI Nachrichten Bits und so #1022 (Wie Weißbier)(06.09.2026 um 20:39 Uhr)
🍏 iOS / Mac OSHue-App 6.0 ist da: das sind die Neuerungen(07.09.2026 um 17:21 Uhr)

🔧 Programmierung 🕛 vor 1 Jahr 5 Min Lesezeit
0

Rock Paper Scissors in Python with Tkinter

↗ Quelle (dev.to)
🗣️ Stimme:

The full code for this project can be found on Michael Linson's GitHub

https://github.com/mykylyn/snake-game/blob/main/userInput/rock_paper.py



That said, let's get started.




CODE
from tkinter import *
import random






To begin, we import the libraries, essentially adding the code other people have already written to our project. We want Tkinter for creating the ui and random to simulating randomness.




CODE
window=Tk()

intro=Label(text="Rock Paper Scissor")
description=Label(text="Enter Rock, Paper, Scissors:")






Setting up some things, we create a Tk object by calling Tk(), which is how a window is made using Tkinter. Additionally, we create some Label objects that will show on-screen with their respective text. Each of these are stored in a variable (with the variable name left of the =) so we can reference them later in our code.




CODE
possible_actions=["rock", "paper", "scissors"]
computer_action=random.choice(possible_actions)






Getting into the code proper, we create an array called possible actions with each of the possible moves (stored here as lowercase strings).




CODE
computer=Label(text="")
user=Label(text="")

display=Label(text="")
entry=Entry()






computer will be used to display the computer's move, user the user's, display the result, and entry will use a Tkinter's Entry object to give a place for text input.




CODE
message="none"
user_action=""






We will end up transferring the knowledge of the correct message to display and the user's input between different parts of code, so we declare variables for them out here to be used later.




CODE
def Check():






This tells python that the code indented and below this line is not literal instructions to do right now, but code that can be used later via the function Check(). Here in Check we want to get the user's action and set message to the correct text based on the computer's action.




CODE
    global message
global user_action
user_action=entry.get()






Since these variables were not defined within the function (and not in the same class) we need to use the global keyword to tell python "yes, I'm sure I really do want you to look around for some other variable named message and user_action," so we can use them within the function.

There are ways to avoid having to use the messy global keyword (classes for example), but for the sake of a quick project we're not building on too much, "it works."

We can get the text typed into entry using entry.get(), which we will read as the user's move of choose.




CODE
    if user_action==computer_action:
message=f"Both player selected {user_action}. It's a tie!"
elif user_action =="rock":
if computer_action == "scissors":
message="Rock smashes scissors! You win!"
else:
message="Paper covers rock! You lose."

elif user_action =="paper":
if computer_action =="rock":
message="Paper covers the rock! You win!"
else:
message="Scissors cuts paper! You lose"

elif user_action == "scissors":
if computer_action=="paper":
message="Scissors cuts paper! You win!"
else:
message="Rock smashes scissors! You lose."






This is a big chunk of text, but is actually not too difficult to understand. If the user and computer picked the same option, we set message to declare a tie (the f at the beginning of the string allows us to insert user_action using the surrounding curly brackets {}). Otherwise, (else and elif statements only run if the previous if or elif statements didn't run) we if the user choose rock, declare a win if the computer choose scissors and a loss otherwise (we already covered the possibility of a tie). On and on for the other two options.

There's many ways to write this code. It's not wrong to just go with straightforward implementations like above, but as you become a better programmer, keep an eye out for ways to condense repetitive code like this (imagine we had 5, 7, or 100 moves, what would you do then?).




CODE
def Enter():
global computer_action
global user_action
Check()
computer["text"]="The computer choose "+computer_action
user["text"]="The user choose "+user_action
display["text"] = message
computer_action=random.choice(possible_actions)






Here we define a function to be called when we want to check the result and update the text. Again using global to gain access to our variables, using Check() (defined above) to set message and user_action to the appropriate values. Then, setting the text of the relevant labels to the appropriate text (although computer, user, and display are also variables, Tkinker objects are always global). Finally, we randomize computer_action again for the next time we play.




CODE
Return = Button(window,text="Enter", padx=2, pady=2, bg="lightgreen", font=("Arial, 10"), command=Enter)






Finally, we create a button with the following properties:




  • is on our window

  • has padx extra horizontal space and pady extra vertical space around the text

  • has bg background color

  • uses the given font based on its name and font size number

  • runs command function when clicked




CODE
intro.pack(fill=X)
description.pack(fill=X)
entry.pack(fill=X, side=BOTTOM)
Return.pack(fill=X, side=BOTTOM)
computer.pack(fill=X, side=BOTTOM)
user.pack(fill=X, side=BOTTOM)
display.pack(fill=X, side=BOTTOM)


window.mainloop()






Putting it all together, remember that our ui elements need to be put on screen with a function such as pack, and that the window's mainloop() needs to be called for it to actually run.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
Hackers Just Poisoned the Rust Supply Chain | Threat Wire
1 Quelle
Hackers Found a Way Into Humanoid Robots | Threat Wire
1 Quelle
Bits und so #1021 (Passwort für Laufwerk)
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Rock Paper Scissors in Python with Tkinter

Thematisch verwandte Begriffe: Rock, Paper, Scissors, Python · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...