So simply, I was trying to create a code that when you press certain buttons, it writes out whatever you put into the Tk input. I was trying to make a hotkey with alt and another letter but whenever I use alt or ctrl, the function does not work and opens up random applications on my screen. everything works fine with keys like shift + a for example, but outputs it all in capitals because of the use of shift. The part where I write the Hotkeys is where all the Hotkey variables are
from tkinter import *
import keyboard, pyautogui
import os
import sys
import tkinter.messagebox
import time
root = Tk()
root.resizable(False, False)
root.iconbitmap('Logo.ico')
root.title(" ")
root.geometry("170x300")
Canvas(root, width=170, height=300)
hotkey1 = "alt + q"
hotkey2 = "alt + w"
hotkey3 = "alt + e"
hotkey4 = "alt + r"
Label(root, text="MacMaker").place(x = 50)
Label(root, text="Type in Macros below:").place(x = 27, y = 21)
Label(root, text="F4:").place(x = 5, y = 48)
Label(root, text="F7:").place(x = 5, y = 78)
Label(root, text="F8:").place(x = 5, y = 108)
Label(root, text="F9:").place(x = 5, y = 138)
thing1 = Entry(width=20)
thing1.place(x = 35, y = 50)
thing2 = Entry(width=20)
thing2.place(x = 35, y = 80)
thing3 = Entry(width=20)
thing3.place(x = 35, y = 110)
thing4 = Entry(width=20)
thing4.place(x = 35, y = 140)
def my_mainloop():
macro1 = thing1.get()
macro2 = thing2.get()
macro3 = thing3.get()
macro4 = thing4.get()
if keyboard.is_pressed(hotkey1):
pyautogui.typewrite(macro1)
elif keyboard.is_pressed(hotkey2):
pyautogui.typewrite(macro2)
elif keyboard.is_pressed(hotkey3):
pyautogui.typewrite(macro3)
elif keyboard.is_pressed(hotkey4):
pyautogui.typewrite(macro4)
root.after(1, my_mainloop)
root.after(1, my_mainloop)
root.mainloop()
From the docs, it looks like it should be:
hotkey1 = "alt+q"
hotkey2 = "alt+w"
hotkey3 = "alt+e"
hotkey4 = "alt+r"
without the spaces
Related
I'm working on a simple registration phase for a password scheme, but I can't seem to get the value in the combobox as an integer as it keeps giving ValueError: invalid literal for int() with base 10: ''. I've tried many methods but they don't seem to work. Additionally, I want to store the data in the entry and comboboxes for my next window, but it keeps registering as ValueError: '' is not in list.
The code:
import tkinter as tk
from tkinter import *
import random
from tkinter import messagebox
from tkinter import ttk
from tkinter.ttk import Combobox
root = tk.Tk()
root.geometry("375x130")
var = tk.StringVar()
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
lbl = tk.Label(root, text = "Your password must have 7 to 15 characters with \nat least 1 number, 1 uppercase and 1 lowercase letter. ")
lbl1 = tk.Label(root, text = "Choose your password: ")
lbl2 = tk.Label(root, text = "n")
lbl3 = tk.Label(root, text = "y")
entry1 = tk.Entry(root)
combo1 = Combobox(root, values = data, state = "readonly")
combo2 = Combobox(root, values = data, state = "readonly")
lbl.place(x = 10, y = 0)
lbl1.place(x = 10, y = 35)
lbl2.place(x = 10, y = 60)
lbl3.place(x = 10, y = 85)
entry1.place(x = 155, y = 35)
combo1.place(x = 25, y = 60)
combo2.place(x = 25, y = 85)
pw = entry1.get()
n = combo1.get()
y = combo2.get()
root.title('Registration')
done_button = tk.Button(root, text = "Done", command = root.destroy, width = 5)
done_button.place(x = 205, y = 80)
root.mainloop()
password = str(pw)
n = int(data.index(n)) - 1
y = int(data.index(y)) - 1
When the following lines are executed:
pw = entry1.get()
n = combo1.get()
y = combo2.get()
The entry box and the two comboboxes are just created, so nothing is input in the entry box and nothing is selected in the two comboboxes.
You need to do it inside a function triggered by the "Done" button:
...
# function called by "Done" button
def done():
pw = entry1.get()
n = int(combo1.get()) # convert selection to integer
y = int(combo2.get()) # convert selection to integer
print(f"{pw=}, {n=}, {y=}")
n = data.index(n) - 1
y = data.index(y) - 1
print(f"{n=}, {y=}")
root.destroy()
root.title('Registration')
done_button = tk.Button(root, text = "Done", command = done, width = 5)
done_button.place(x = 205, y = 80)
root.mainloop()
Output in console:
pw='hello', n=4, y=9
n=2, y=7
There are a couple of issues with your code:
Firstly, you need to bind the combobox change events with a function of some sort
The combobox returns a string of the number selected - which needs conversion
I am not sure what you aimed to do with n and y - but I kept the formula you used.
Fix these, and the combobox issues resolve.
See code below:
Code:
import tkinter as tk
from tkinter import *
import random
from tkinter import messagebox
from tkinter import ttk
from tkinter.ttk import Combobox
def combo_n_changed(event):
n = combo1.get()
n = int(data.index(int(n))) - 1
print("n=",n)
def combo_y_changed(event):
y = combo2.get()
y = int(data.index(int(y))) - 1
print("y=",y)
root = tk.Tk()
root.geometry("375x130")
var = tk.StringVar()
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
lbl = tk.Label(root, text = "Your password must have 7 to 15 characters with \nat least 1 number, 1 uppercase and 1 lowercase letter. ")
lbl1 = tk.Label(root, text = "Choose your password: ")
lbl2 = tk.Label(root, text = "n")
lbl3 = tk.Label(root, text = "y")
entry1 = tk.Entry(root)
combo1 = Combobox(root, values = data, state = "readonly")
combo2 = Combobox(root, values = data, state = "readonly")
lbl.place(x = 10, y = 0)
lbl1.place(x = 10, y = 35)
lbl2.place(x = 10, y = 60)
lbl3.place(x = 10, y = 85)
entry1.place(x = 155, y = 35)
combo1.place(x = 25, y = 60)
combo2.place(x = 25, y = 85)
pw = entry1.get()
combo1.bind('<<ComboboxSelected>>', combo_n_changed)
combo2.bind('<<ComboboxSelected>>', combo_y_changed)
root.title('Registration')
done_button = tk.Button(root, text = "Done", command = root.destroy, width = 5)
done_button.place(x = 205, y = 80)
root.mainloop()
I entered the information as per the image below:
Output:
n= 1
y= 2
I couldnt seem to find a way to make my program realise that ive selected a button, so i changed the function of the celcius to farenheit to try to make it change a boolean value to determine what conversion the program is doing
def celcius_to_farenheit(_event = None):
c_to_f = True
f_to_c = False
the idea being later i can use if statments later in the end result function to find what conversion its doing and display results in the status bar
def end_result():
if c_to_f == True:
converted_temperature = (valid_temperature * 9/5) + 32
label_status.configure(text = converted_temperature, fg = "Orange")
currently i seem to have functions running without me pressing buttons as well, when start the program it immediatly goes to the error message ive created for input muct be numeric even if i havent pressed the celcius to farenheit button.
Any help regarding how to propely have my celcius to farenheit and farenheit to celcius buttons confirm its a float and change a value to use for determining which calculation its using would be helpfull. Knowing why the error message comes up automatically is a bonus.
Below is my code thank you for your time and help.
import sys
from tkinter import *
from tkinter.tix import *
c_to_f = True
def clear_reset(_event = None):
entry_temperature.delete(0, END)
label_status.configure(text = "All data cleared", fg = "Orange")
def end_program(_event = None):
sys.exit()
def convert_temp(_event = None):
try:
valid_temperature = float(entry_temperature.get())
except:
label_status.configure(text = "Input must be numeric", fg = "Orange")
def end_result():
if c_to_f == True:
converted_temperature = (valid_temperature * 9/5) + 32
label_status.configure(text = converted_temperature, fg = "Orange")
def celcius_to_farenheit(_event = None):
c_to_f = True
f_to_c = False
def farenheit_to_celcius(_event = None):
f_to_c = True
c_to_f = False
window = Tk()
window.geometry("550x200")
window.resizable(False, False)
window.title("Temperature Conversion")
tooltip = Balloon(window)
label_input_Temperature = Label(text = "Temperature",fg = "Green")
label_input_Temperature.grid(row= 0, column=0)
entry_temperature = Entry(window, bg = "light blue" )
entry_temperature.grid(row=0, column=1)
temp_button_c_to_f = Button(window, text = "Celcius to Farenheit", command = celcius_to_farenheit)
temp_button_c_to_f.grid(row = 1, column=0)
window.bind('<Shift-c>', celcius_to_farenheit)
tooltip.bind_widget(temp_button_c_to_f, msg = "Shift + C")
temp_button_f_to_c = Button(window, text = "Farenheit to Celcius")
temp_button_f_to_c.grid(row = 1, column = 1 )
conversion_button = Button(window, text = "Convert", command = convert_temp)
conversion_button.grid(row = 2, column = 0,padx =0 )
window.bind('<Enter>', convert_temp)
tooltip.bind_widget(conversion_button, msg = "Enter")
clear_button = Button(window, text = "Clear", command = clear_reset)
clear_button.grid(row = 2, column = 1)
window.bind('<Control-c>', clear_reset)
tooltip.bind_widget(clear_button, msg = "Ctrl + C")
exit_button = Button(window, text = "Exit")
exit_button.grid(row = 2, column = 2, padx = 20, pady = 20)
window.bind('<Control-x>', end_program)
tooltip.bind_widget(exit_button, msg = "Ctrl + X")
label_status = Label(window, width = 50, borderwidth = 2, relief= RIDGE,bg= "Grey" )
label_status.grid(row = 4, column = 1)
tooltip.bind_widget(label_status, msg = "Displays results / error messages")
label_status.configure(text = "Enter in your temperature and select your conversion", fg = "Orange")
window.mainloop()
I want to decrease the size of an entry in tkinter python. ive already asked about label but i havent used fyi
the code:
from tkinter import *
from tkinter import messagebox
top = Tk()
top.title("Hello wold")
top.geometry("300x300")
ei = StringVar()
ei1 = StringVar()
def karma():
try:
ee = ei.get()
ee1 = ei1.get()
ez = int(ee) + int(ee1)
return messagebox.showinfo('message', f'{ee} + {ee1} = {ez}')
except ValueError:
return messagebox.showinfo('message', f'ERROR')
name = Label(top,text="1st no. : ").place(x = 20,y = 50)
e = Entry(top,textvariable=ei).place(x = 100, y = 50)
name1 = Label(top,text="2nd no. : ").place(x = 20,y = 100)
e1 = Entry(top,textvariable=ei1).place(x = 100, y = 100)
b = Button(top, text="Click here", command=karma).place(x = 20,y = 150)
top.mainloop()
you may use the font option in Entry to adjust the size.
Here is an example (to change it to 8pts)
import tkFont
helv8 = tkFont.Font(family='Helvetica', size=8)
e = Entry(top, font = helv8, textvariable=ei).place(x = 100, y = 50)
My program stops working after successfully running the two exes. here is the code:
from tkinter import *
import os
root = Tk()
root.geometry('350x150')
root.title("hurler")
photo = PhotoImage(file = "Logo_Image.png")
root.iconphoto(False, photo)
entry_text = Label(text = "Number of posts you wish to automate (between 1-12) * ")
entry_text.place(x = 15, y = 10)
num = StringVar()
time_entry = Entry(textvariable = num, width = "10")
time_entry.place(x = 15, y = 40)
def action():
global num
num = num.get()
if num == '1':
os.startfile('.\\hurl\\hurl.exe')
# exit()
if num == '2':
os.startfile('.\\hurl\\hurl.exe')
os.startfile('.\\hurl2\\hurl.exe')
# exit()
num = StringVar()
register = Button(root,text = "Make", width = "10", height = "2", command = action, bg = "lightblue")
register.place(x = 15, y = 70)
root.mainloop()
hurl takes in text entries and then runs a web driver exe after pressing the Post button. Heres a sample block from code from hurl:
from tkinter import *
import os
from time import sleep
root = Tk()
root.geometry('600x600')
root.title("hurl")
photo = PhotoImage(file = "Logo_Image.png")
root.iconphoto(False, photo)
def save_post():
emailE = email_in.get()
file1 = open ("user.txt", "w")
file1.write(emailE)
file1.close()
passE = pass_in.get()
file2 = open ('pass.txt', 'w')
file2.write(passE)
file2.close()
entry1_text = Label(text = "Email * ",)
entry2_text = Label(text = "Password * ",)
entry1_text.place(x = 15, y = 70)
entry2_text.place(x = 15, y = 130)
email_in = StringVar()
pass_in = StringVar()
email_entry = Entry(textvariable = email_in, width = "30")
pass_entry = Entry(textvariable = pass_in, width = "30")
def app():
os.system(".\Auto_Post_With_Photo.exe")
def everything():
save_post()
app()
register = Button(root,text = "Post", width = "10", height = "2", command = everything, bg = "lightblue")
register.place(x = 15, y = 380)
root.mainloop()
I'm hoping I don't need to show the last program to get an answer to the issue. The Issue I'm having is that the program runs perfectly until the button is pressed, and then the hurls exes crash.
But if I click on the two hurl.exe with my mouse and not this 3rd program pressing the post button won't crash.
Any ideas to fix this issue? Also, it's not due to the 'exit()s', it does the same thing with or without them. :)
I'm trying to set an input to Combobox(values = output) from other
function (which connected to Button(command = some_function)
from tkinter import ttk
from tkinter import filedialog
from tkinter import *
def select():
global sel
a = ['101','102','103','104','105']
b = ['201','202','203','204','205']
sel = []
#label.configure(text = " Fleet" + fleet.get())
choosed = fleet.curselection()
for i in choosed:
selection = fleet.get(i)
print ("selected " + " " + selection)
if selection == 'B':
sel = b
else: sel = a
#print (sel)
return sel
root =Tk()
fleet = Listbox(root, width = 10, height = 2)
fleet.insert(1, 'B')
fleet.insert(2, 'A')
fleet.grid(column = 1, row = 0)
label = ttk.Label(root, text = "Please choose the fleet")
label.grid (column = 0, row = 0)
button1 = ttk.Button(root, text = 'Select', command = select)
button1.grid(column = 0, row = 1)
a = ['101','102','103','104','105']
b = ['201','202','203','204','205']
combo_tool_num = ttk.Combobox(root, width = 10, values = sel)
I would like to set an select() output sel, as an input for: combo_tool_num values = sel.
Thank you!
To set initial value in combobox use 'set()'
Use syntax as,
combo_tool_num = ttk.Combobox(root, width = 10, values = sel)
combo_tool_num.set('Select')