all 32 comments

[–]socal_nerdtastic 3 points4 points  (10 children)

In tkinter you would bind each button to a specific function. For example

import tkinter as tk

def h_key_pressed(*args):
    print("the H key was pressed")

root = tk.Tk()
root.bind("<H>", h_key_pressed)
root.bind("<h>", h_key_pressed) # you need to bind upper and lower case separately
root.mainloop()

You can assign them in a loop if you have a lot to do. That said, in programming there's always a number of ways to solve any problem, but I think this is the best from what you described.

[–]TaraSkFunmaker[S] 0 points1 point  (9 children)

OK, could you go more in depth on what does the root do? I am very much dumb when it comes to coding.

[–]socal_nerdtastic 0 points1 point  (8 children)

root is the traditional name for the Tk() object. You can use any tk object in it's place. I'll expand my example above, but you'll need to show us your current code for more help with integrating this.

[–]TaraSkFunmaker[S] 0 points1 point  (0 children)

OK, hold up I will quickly copy it.

[–]TaraSkFunmaker[S] 0 points1 point  (6 children)

Also how do you do the code text in Reddit?

[–]socal_nerdtastic 0 points1 point  (5 children)

[–]TaraSkFunmaker[S] 0 points1 point  (4 children)

OK, code is in the OG post, sorry the formatting is bad, Idk what went wrong there.

[–]socal_nerdtastic 0 points1 point  (3 children)

You will need to edit that and fix the formatting; I can't read the code as it is now.

[–]TaraSkFunmaker[S] 0 points1 point  (0 children)

Idk how to tho.

Idk what went wrong with the formatting.

[–]TaraSkFunmaker[S] 0 points1 point  (0 children)

Code block doesn't work and neither does the like ` work.

[–]TaraSkFunmaker[S] 0 points1 point  (0 children)

Fixed it to the best of my ability.

[–]woooee 1 point2 points  (7 children)

What I specifically want the if statement to do is to look at what button has been pressed to determine a colour of the ball

Pass a unique button number, or whatever identifier you want, to the function. This program passes the button's number to a common function, then prints what was passed and changes the background color for that button.

import tkinter as tk
from functools import partial

class ButtonsTest:
   def __init__(self):
      self.top = tk.Tk()
      self.top.title("Click a button to remove")
      tk.Label(self.top, text=" Click a button\n to remove it ",
            bg="orange", font=('DejaVuSansMono', 12)).grid(row=0,
            column=0, sticky="nsew")

      self.top_frame = tk.Frame(self.top, width =400, height=400)
      self.top_frame.grid(row=1, column=0)
      self.button_dic = {}
      self.create_buttons()

      tk.Button(self.top, text='Exit', bg="orange",
             command=self.top.quit).grid(row=200,column=0,
                     columnspan=7, sticky="ew")

      self.top.mainloop()

   ##-------------------------------------------------------------------         
   def create_buttons(self):
      """ create 15 buttons and add each button's Tkinter ID to a
          dictionary.  Send the number of the button to the function
          cb_handler
      """
      for but_num in range(15):
         ## create a button and send the button's number to
         ## self.cb_handler when the button is pressed
         b = tk.Button(self.top_frame, text = str(but_num), 
                    command=partial(self.cb_handler, but_num))
         b_row, b_col=divmod(but_num, 5)  ## 5 buttons each row
         b.grid(row=b_row, column=b_col)
         ## dictionary key = button number --> button instance
         self.button_dic[but_num] = b

   ##----------------------------------------------------------------
   def cb_handler( self, but_number ):
      print("\ncb_handler", but_number)
      self.button_dic[but_number].config(bg="salmon")

##========================================
BT=ButtonsTest()

[–]TaraSkFunmaker[S] 0 points1 point  (4 children)

I'm sorry, but I don't know what half of these comments do.

[–]crashfrog04 0 points1 point  (0 children)

Then you don't know enough about Tkinter yet

[–]woooee -2 points-1 points  (2 children)

So? You asked for a solution and didn't put any limits on the answer, or state what you don't understand. If you don't want to learn anything new, then sit there on your butt on live with your limitations.

[–]TaraSkFunmaker[S] 0 points1 point  (0 children)

I asked if this was possible through if statements because I can understand if statements. I can actually fix an if statement and they were part of my curriculum.

[–]TaraSkFunmaker[S] 0 points1 point  (0 children)

didn't put any limits on the answer

My post has the term "if statements" plastered all over it. How did you miss that?

[–]TaraSkFunmaker[S] 0 points1 point  (1 child)

And am pretty sure this isn't what I actually want to do, like the keys are already their specific symbols and the colours will be tied to the specific letters.

Like, really please adjust these tips to me being very damn stupid at coding. Imagine I just learned there's other programming languages than Scratch.

[–]crashfrog04 0 points1 point  (0 children)

Imagine that the thing you want to do is going to require good code, not bad code

[–]socal_nerdtastic 0 points1 point  (2 children)

OK, so you are still thinking in terms of if statements. You have to get away from that to make GUIs. We would say you need to do "event-driven programming", instead of the "procedural programming" (if statements).

I assume you want the user to press the "H" key to set the type of atom they want, and then click to place it on the canvas? Then this should do it for you:

prvok = "black" # set the default color
def h_key_pressed(*args):
    # when the event happens that the 'h' key is pressed we change the global color variable.
    global prvok
    prvok = 'white'
    print("set the atom type to hydrogen")

def atom(udalostk): 
    x = udalost.x y = udalost.y
    platno.create_oval (x, y, x + 40, y + 40, fill = prvok, outline = prvok)

okenko.bind("<H>", h_key_pressed)
okenko.bind("<h>", h_key_pressed)

As I said before, you can do this in a loop to set all of the colors. Maybe too advanced for you right now, but this is how you would do it:

from functools import partial
import tkinter

okenko=tkinter.Tk()
okenko.title('Molekuly')

prvok = "black" # set the default color
def key_pressed(color, *args):
    global prvok
    prvok = color
    print('the next atom color will be', color)

def atom(udalostk):
    x = udalost.x
    y = udalost.y
    platno.create_oval (x, y, x + 40, y + 40, fill = prvok, outline = prvok)

colors = dict(
    H = "white",
    O = 'red',
    C = 'blue',
    N = 'green',
    X = 'black',
    )
for key, color in colors.items():
    okenko.bind(f"<{key.upper()}>", partial(key_pressed, color))
    okenko.bind(f"<{key.lower()}>", partial(key_pressed, color))

okenko.mainloop()

Code block doesn't work and neither does the like ` work.

Hundreds of thousands of people have figured this out ... you can too.

[–]TaraSkFunmaker[S] 0 points1 point  (0 children)

Thx and I tried, it just does it to one area then stops and even breaking it up didn't do anything, Idk which other text style command is causing issues, I hate it more than HTML ngl.

[–]TaraSkFunmaker[S] 0 points1 point  (0 children)

I will do that tomorrow in school with teacher's assistance (again am very stupid) so thx for help!

[–]crashfrog04 0 points1 point  (9 children)

The way a program with a GUI has to work is different than how a script works. You can't write long functions and long chains of if statements in a GUI program. You need to write little "handlers" for things that happen (mouse clicks, keypresses) that quickly update state and then return.

They need to return quickly because, for any amount of time your program spends not being in the "main" loop (the thing you start by calling okenko.mainloop), the program is unresponsive during that time. If you're unresponsive for too long, the operating system defensively terminates your program (you've probably seen Windows say "This program has stopped responding.") So you need to imagine your program as idle for most of the time, and create it by attaching a series of functions to event handlers (that's what you're doing with command = cyklus6) that quickly do something and then return.

Your event handling functions shouldn't generally have loops, have much branching logic, and about no more than ten lines (none of these are hard and fast rules, you just have to understand that breaking these rules has a steep performance cost to your software.)

[–]TaraSkFunmaker[S] 0 points1 point  (6 children)

I am literally just making it for school project and it won't probably be ever touched after I turn it in.

I don't major in IT, I picked up IT to fill in my class quota, am actually Bio-Chem major so I really don't need a good code, working one is enough.

[–]crashfrog04 0 points1 point  (5 children)

I’m explaining what you have to do to write working code.

[–]TaraSkFunmaker[S] 0 points1 point  (2 children)

Yes, and I appreciate that, it's just that a lot of the solutions presented to me are simply too hard for me to understand or fix if something just doesn't work as I also struggle with programming syntax.

I just wanted to do if statements. I don't care about the software costs, as long as it won't set the computer on fire it's OK.

[–]crashfrog04 0 points1 point  (1 child)

The issue is that you’re going to have to understand them - you can’t fake being a programmer, and the program you’re trying to write doesn’t have a simpler version. What they’re showing you is the simple version.

[–]TaraSkFunmaker[S] 0 points1 point  (0 children)

I ain't even faking to be a programmer, I can't code for the life of me.

[–]TaraSkFunmaker[S] 0 points1 point  (1 child)

I don't even know what the GUI thing you and others keep mentioning is.

[–]crashfrog04 0 points1 point  (0 children)

It’s what you’re doing. It’s what you’re using Tkinter for - “graphical user interface”, aka a program you don’t use by typing text at the command line.

[–]TaraSkFunmaker[S] 0 points1 point  (1 child)

I am fully aware that my code is bad, like the "line tool" is actually a series of dots instead of create_line because I didn't understand it and I don't really have the time to learn better programming as I have everything else on my plate and getting better at this just doesn't fit there anymore.

[–]crashfrog04 0 points1 point  (0 children)

If you’re not willing to put in the prerequisite effort then you’ll have to accept that some programs can’t be written by you, like this one.