Python Forum
[Tkinter] Python - Tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Python - Tkinter
#1
Hi, I'm doing Python with Tkinter.

So, I've a program :

from tkinter import *

root = Tk()

def fonction(event):
    print(event)
        
root.bind('a', fonction)
root.bind('b', fonction)



root.mainloop()

input()
This program take what the users click on his keyboard.

But I've 2 problems :

(I've IDLE Python 3.6 32 bits)

When I click on 'Run Program' in the edition windows, the program run.
But, when I double clic on it, in the folder, the black window open, and shut up. I don't know why and I can't see what is written ...

And is it possible to run it in bottom task ? Like when the users can't see it or just when the users don't click on it. Like, when you are surfing on Internet, and you press "A" and my program start and run an other program ? Because if not, I can use input() ...

Thanks

I'm sorry for the bad english ...
Reply
#2
IDLE is a poor choice of IDE, and may be the reason you are haviing problems.
try saving as a .py file (file-->New File, then paste code ans save)
then try to run from command line with:
python programname.py
FYI: suggest VSCode see: https://python-forum.io/Thread-VS-Code-f...ght=VSCode
Reply
#3
So, can we run it in bottom task ?

Here is my new program :

from tkinter import *

root = Tk()

def ecrire(event):
    print(event.char, end = "")

root.bind('a', ecrire)
root.bind('b', ecrire)
root.bind('c', ecrire)
root.bind('d', ecrire)
root.bind('e', ecrire)
root.bind('f', ecrire)
root.bind('g', ecrire)
root.bind('h', ecrire)
root.bind('i', ecrire)
root.bind('j', ecrire)
root.bind('k', ecrire)
root.bind('l', ecrire)
root.bind('m', ecrire)
root.bind('n', ecrire)
root.bind('o', ecrire)
root.bind('p', ecrire)
root.bind('q', ecrire)
root.bind('r', ecrire)
root.bind('s', ecrire)
root.bind('t', ecrire)
root.bind('u', ecrire)
root.bind('v', ecrire)
root.bind('w', ecrire)
root.bind('x', ecrire)
root.bind('y', ecrire)
root.bind('z', ecrire)
root.bind('0', ecrire)
root.bind('1', ecrire)
root.bind('2', ecrire)
root.bind('3', ecrire)
root.bind('4', ecrire)
root.bind('5', ecrire)
root.bind('6', ecrire)
root.bind('7', ecrire)
root.bind('8', ecrire)
root.bind('9', ecrire)


root.mainloop()
So, the program just run if the "tkinter window" (with the feather) is open (if we clic on it and we don't click anywhere else). But can we set something in the program which will make the program run even if the "tkinter window" isn't open ?

Here is my "new new" program :

He collect the keyboard, space, letters, and numbers. And I find a way to hide the feather window. But if I click somewhere else, the program don't run.

import tkinter as tk

root = tk.Tk()
root.withdraw()


def ecrire(event):
    print(event.char, end = "")

root.bind('a', ecrire)
root.bind('b', ecrire)
root.bind('c', ecrire)
root.bind('d', ecrire)
root.bind('e', ecrire)
root.bind('f', ecrire)
root.bind('g', ecrire)
root.bind('h', ecrire)
root.bind('i', ecrire)
root.bind('j', ecrire)
root.bind('k', ecrire)
root.bind('l', ecrire)
root.bind('m', ecrire)
root.bind('n', ecrire)
root.bind('o', ecrire)
root.bind('p', ecrire)
root.bind('q', ecrire)
root.bind('r', ecrire)
root.bind('s', ecrire)
root.bind('t', ecrire)
root.bind('u', ecrire)
root.bind('v', ecrire)
root.bind('w', ecrire)
root.bind('x', ecrire)
root.bind('y', ecrire)
root.bind('z', ecrire)
root.bind('0', ecrire)
root.bind('1', ecrire)
root.bind('2', ecrire)
root.bind('3', ecrire)
root.bind('4', ecrire)
root.bind('5', ecrire)
root.bind('6', ecrire)
root.bind('7', ecrire)
root.bind('8', ecrire)
root.bind('9', ecrire)
root.bind('<space>', ecrire)
root.bind('A', ecrire)
root.bind('B', ecrire)
root.bind('C', ecrire)
root.bind('D', ecrire)
root.bind('E', ecrire)
root.bind('F', ecrire)
root.bind('G', ecrire)
root.bind('H', ecrire)
root.bind('I', ecrire)
root.bind('J', ecrire)
root.bind('K', ecrire)
root.bind('L', ecrire)
root.bind('M', ecrire)
root.bind('N', ecrire)
root.bind('O', ecrire)
root.bind('P', ecrire)
root.bind('Q', ecrire)
root.bind('R', ecrire)
root.bind('S', ecrire)
root.bind('T', ecrire)
root.bind('U', ecrire)
root.bind('V', ecrire)
root.bind('W', ecrire)
root.bind('X', ecrire)
root.bind('Y', ecrire)
root.bind('Z', ecrire)

root.mainloop()
And, if I double click, the black window appears, and the feather window too, (when I don't use the tk.Tk and root.withdraw. But the black window don't run, there is nothing written ...
Reply
#4
You can use <Key> instead of binding every key. http://effbot.org/tkinterbook/tkinter-ev...ndings.htm
Reply
#5
What are you trying to accomplish? It sounds like it's working, but that you want it to run without a window or console, is that correct? Is this for a keylogger or something?
Reply
#6
Ok thanks !

Yes, the program works well. But, I want to make my own "shortcut" with Python. Like, when the users is surfing on Internet, he wants to open a "Notepad" so (First of all he start the program at the opening of the session) he write anywhere "Notepadstart" and my Python start Notepad. Now, my program works, but only if the Python window is open. So it don't work if the user is surfing on Internet. So, it's NOT a keylogger (because it's my own computer and it's for shortcut, and if I really want a keylogger, I think that I will know how to download one rather than create one, which will be so bad ...).

So, is it possible to run it without a window or console ? Like you said nilamo.
Thanks
Reply
#7
https://superuser.com/questions/62525/ru...hidden-way

That's not really a python issue, so much as it is a Windows one. So the workaround, is using a vb script that will start the application without also starting an terminal session.

OR, when (...if) you bundle it as an exe, things like PyInstaller have options for running in Windowed mode, instead of console mode.
Reply
#8
Thanks ! It hides the window...

But, the program don't run if I click somewhere else ...

So, have you any idea ?

Thanks !
Reply
#9
Hi again,

I download Pygame. But I saw somewhere that it was impossible, like Tkinter, to run it without the Window (everywhere in every window (like Firefox ...)). Is it true ?

Thanks
Reply
#10
Hello guys. I have a small problem with my app(It's not big ,just for experience).
As you can see in the first photo ,when I start my program ,all radiobuttons has a gray color inside(Its not selected),but it has no the default white color I want.When I select a choice,it's white(ok) .So,how can I fix this problem?
Also, as you can see in the 2nd photo when (for example) choose "orange juice" (a big word) and then I choose "Meat" (small word) ,the printing of the word is not obvious. How can I fix this problem?

1st photo
2nd photo

Code:
from tkinter import *

root = Tk()
root.title("Super Market")
root.geometry("1500x1300")

food = [
        ("Rice" , 0.99) ,                       # value for radio_button -> 1
        ("Spaghetti" , 0.95) ,                  # value for radio_button -> 2
        ("Ice cream" , 3.44) ,                  # value for radio_button -> 3
        ("Pizza" , 7.50) ,                      # value for radio_button -> 4
        ("Cheese" , 2.10) ,                     # value for radio_button -> 5
        ("Cheddar" , 0.45) ,                    # value for radio_button -> 6
        ("Cherry" , 0.88) ,                     # value for radio_button -> 7
        ("Banana" , 0.40) ,                     # value for radio_button -> 8
        ("Apple" , 0.67) ,                      # value for radio_button -> 9
        ("Meat" , 7.28) ,                       # value for radio_button -> 10
        ("Fish" , 9.72) ,                       # value for radio_button -> 11
        ("Orange" , 0.62) ,                     # value for radio_button -> 12
        ("Orange juice" , 2.10) ,               # value for radio_button -> 13
        ("Chocolate" , 0.99) ,                  # value for radio_button -> 14
        ("Bread" , 0.55)                        # value for radio_button -> 15
       ]


food_name_var = StringVar()

frame = LabelFrame(root , relief = SUNKEN , bd = 1)
frame.grid(row = 0 , column = 0 , sticky = W)

r = 0
for food_name , food_price in food:
        Radiobutton(frame , text = food_name , variable = food_name_var , value = r+1 , padx = 10 , pady = 5).grid(row = r , column = 0 , sticky = W)
        label_price = LabelFrame(root , relief = SUNKEN , bd = 1 , text = "Price")
        label_price.grid(row = r , column = 1 , sticky = W)
        r += 1
        

def select_food(food_name):
        label_print_food = Label(root , text = food_name)
        label_print_food.grid(row = 0 , column = 4)
        

button = Button(root , text = "Basket" , command = lambda: select_food(food[ int(food_name_var.get() ) - 1][0]) )
button.grid(row = 0 , column = 3)


root.mainloop()
Thanks in advance Smile
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020