#๐Ÿ”’ how to i create a window in python?

92 messages ยท Page 1 of 1 (latest)

topaz idol
#

i want to create a window where if i press a key, it closes (ik its basic but im unintelligent)

deft quailBOT
#

@topaz idol

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

dense moat
#

Look into tkinter

#

Or turtle module

topaz idol
#

thanks

dense moat
#

No problem

topaz idol
#

man why is python so reliant on packages

#

want key inputs? need to get pynput

dense moat
topaz idol
#

want to make a window? need to get something

dense moat
#

The hard work has been done for you

brazen oak
dense moat
#

Other languages, such as C++ have few libraries and you need to code everything by yourself

brazen oak
#

it's the ecosystem that makes python so popular and powerful

topaz idol
#

interesting

dense moat
#

Sure if you want to make a window, you can do it, but you will do it in hundreds of lines

#

With packages, in a few lines you are done

#

For example with turtle

brazen oak
dense moat
#
from turtle import Screen
screen = Screen()
screen.exitonclick()

Would make you exit the screen when it's you click on the window, with an if statement probably you can do what you want

topaz idol
#

so how do i install them

#

im using pycharm

dense moat
topaz idol
#

and when i write py -im turtle or py -im tkinter they dont work

#

oh

dense moat
#

No need to install it

topaz idol
#

thats great then

dense moat
#

Yeah

#

There are some packages that come with python

topaz idol
#

so i could make it work with pynput to close the screen when i press Q?

frosty gorge
brazen oak
#

unless you use the PyCharm GUI for it you can also go to the console and make sure your venv is active and do

py -m pip import
topaz idol
#

thats rlly cool

topaz idol
digital lodge
topaz idol
#

hmmmmm, so why doesnt this work?

from pynput import keyboard
from turtle import Screen

while Screen():
 def on_press(key):
     if key == keyboard.Key.A:
         exit(Screen())```
brazen oak
#

you don't want Screen() as the while condition and not inside the exit()either

topaz idol
#

it would work while the screen is on no?

digital lodge
#

I kinda suck at python, but you might be able to do

from turtle import screen

screen = Screen()

def closeScreen():
screen.exit()
screen.listen()

screen.onkeypress(closeScreen, โ€œAโ€)

dense moat
#

Screen() is a class, not a function so it won't do anything but it will give you the options to do so

topaz idol
#

ok so im testing out some attributes, and im running into a problem where i run the script, and the screen appears for like 1/10 of a second then dissapears

topaz idol
#

is a class just an object type????

dense moat
#

A class is a collection of functions and attributes mainly

topaz idol
#

ooooooooooh

#

alright

#

can you help me with the problem rq

dense moat
#

I just woke up after not sleeping 28 hours

topaz idol
#

go to sleep

dense moat
#

I cannot, I gotta sleep at night too because I have school tomorrow

past glen
topaz idol
#

holy

brazen oak
#

yeah, "bring your own batteries" ๐Ÿ˜‰

#

Go on the other hand got all kinds of batteries included

toxic zephyr
topaz idol
#

e.g

 from tkinter import (???)```
topaz idol
#

knowledgable*

#

WHAT IS A FUN ARGUEMENT

#

WHAT IN TARNATION

#

IS A FUN ARGUEMENT

#
from turtle import *
from turtle import Screen
from pynput import *
from pynput import keyboard

screen = Screen()

screen
screen.bgcolor("black")

if screen.onclick():
    screen.setup(900, 200)```
toxic zephyr
#

what gave you the idea to use if screen.onclick(): ?

topaz idol
#

if there is a clcik

toxic zephyr
#

...

topaz idol
#

click*

#

when screen is screen

toxic zephyr
#

that's not how it works, it kind of seems like you are throwing random things at the wall and hoping they work

toxic zephyr
#

how much programming have you done?

topaz idol
#

a few scratch games

#

dipped my toes

toxic zephyr
#

I think if you are new, i would recommend to do some kind of structured learning, like cs50x or cs50p classes, those are great for beginners

toxic zephyr
#

free classes by harvard,
cs50x is intro to programming: some scratch (just first thing, then some C, python, html, css, javascript) it sort of introduces you to things conceptually across a few languages - very good for building foundations and mental map of how to think in general programming

cs50p is intro to python: its purely python and more about learning the language syntax than conceptual things, a bit easier overall, but both are good intros

topaz idol
#

i know more than a few programming concepts

#

so i guess i'll take the python one

#

thanks

compact lion
#

That'd be my guess

deft quailBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.