I have made a python gui using tkinter and i have been running it through the commandline. But when i tried to open it by double clicking, it shortly shows the window but exits again. I tried commenting out all the places where it uses images thinking that it is because it dosent have file-editing permissions when its run by double click and it worked! But i need to use images in the gui, anyone have solutions?
import tkinter as tk
from PIL import Image, ImageTk
import time
root = tk.Tk()
root.title("Krypto Coin")
root.iconbitmap('icon.ico')
root.configure(bg="#112233")
Frame = tk.Frame(root, width=1200, height=800, bg="#112233")
Frame.grid(columnspan=3, rowspan=5)
icon = Image.open('icon_round.png')
icon = ImageTk.PhotoImage(icon)
icon_label = tk.Label(image=icon, borderwidth=0, highlightthickness=0)
icon_label.place(relx=0.5, rely=0.2, anchor="center")
text = tk.Label(root, text="Welcome to Krypto Coin (KTC)", bg="#112233", fg="#ffffff", font=("Arial", 32))
text.place(relx=0.5, rely=0.45, anchor="center")
text = tk.Label(root, text="Enter id of the receiver and amount", bg="#112233", fg="#ffffff", font=("Arial", 24))
text.place(relx=0.5, rely=0.5, anchor="center")
def trasfer_button_pressed():
time.sleep(0.1)
trasfer_button_text.set("Loading...")
trasfer_button.configure(state="disable")
trasfer_button.configure(bg="#112233")
input_id.configure(state="disable")
input_amount.configure(state="disable")
def trasfer_button_enter(e):
if trasfer_button_text.get() == "Transfer":
trasfer_button.configure(bg="#334455")
def trasfer_button_leave(e):
if trasfer_button_text.get() == "Transfer":
trasfer_button.configure(bg="#223344")
trasfer_button_text = tk.StringVar()
trasfer_button = tk.Button(root, textvariable=trasfer_button_text, command=lambda:trasfer_button_pressed(), bg="#223344", fg="#ffffff", width=15, height=1, font=("Arial", 24), borderwidth=0, highlightthickness=0)
trasfer_button_text.set("Transfer")
trasfer_button.place(relx=0.5, rely=0.65, anchor="center")
trasfer_button.bind("<Enter>", trasfer_button_enter)
trasfer_button.bind("<Leave>", trasfer_button_leave)
input_id = tk.Entry(root, width=48, font = "Arial 14", bg="#334455", fg="#ffffff", borderwidth=0, highlightthickness=0)
input_id.insert(0, "ID")
input_id.place(relx=0.45, rely=0.8, anchor="center", height=30)
input_amount = tk.Entry(root, width=20, font = "Arial 14", bg="#334455", fg="#ffffff", borderwidth=0, highlightthickness=0)
input_amount.insert(0, "Amount")
input_amount.place(relx=0.65, rely=0.8, anchor="center", height=30)
root.mainloop()
Related
Python TKinter Mac: Buttons layout strangely and when a button is clicked it looks the same (as if it hadn't been clicked) however I didn't check to see if the button worked.
and the space is a textbook
the Mac version is: a 2017 iMac running Monterey 12.4
import tkinter as tk
window = tk.Tk()
window.geometry("500x500")
window.title("Stock Watchlist & Logger")
label = tk.Label(window, text="Tester", font=('Ariel', 18))
label.pack(padx=20, pady=20)
textbox = tk.Text(window, height=3, font=('Ariel', 16))
textbox.pack(padx=10, pady=10)
numbuttframe = tk.Frame(window)
numbuttframe.columnconfigure(0, weight=1)
yesnobuttframe = tk.Frame(window)
yesnobuttframe.columnconfigure(1, weight=1)
button1 = tk.Button(numbuttframe, text="1", font=('Arial', 18))
button1.grid(row=0, column=0, sticky=tk.W+tk.E)
button2 = tk.Button(numbuttframe, text="2", font=('Arial', 18))
button2.grid(row=0, column=1, sticky=tk.W+tk.E)
button3 = tk.Button(numbuttframe, text="3", font=('Arial', 18))
button3.grid(row=0, column=2, sticky=tk.W+tk.E)
button4 = tk.Button(numbuttframe, text="4", font=('Arial', 18))
button4.grid(row=0, column=3, sticky=tk.W+tk.E)
button5 = tk.Button(numbuttframe, text="5", font=('Arial', 18))
button5.grid(row=0, column=4, sticky=tk.W+tk.E)
button6 = tk.Button(numbuttframe, text="6", font=('Arial', 18))
button6.grid(row=0, column=5, sticky=tk.W+tk.E)
yesbutton = tk.Button(yesnobuttframe, text="Yes", font=('Arial', 18))
yesbutton.grid(row=0, column=0, sticky=tk.W+tk.E)
nobutton = tk.Button(yesnobuttframe, text="No", font=('Arial', 18))
nobutton.grid(row=0, column=1, sticky=tk.W+tk.E)
numbuttframe.pack(fill='x')
yesnobuttframe.pack(fill='x')
if button1:
print("CELEBRATE!")
window.mainloop()
You set the weight to two cells weight=1 , so they expanded, since the rest have a default weight of 0. Since I don’t see your grid - the buttons are placed in frames one after the other, suggest using the pack method. And shortened your code a bit. The buttons won't work yet, because they don't have a command = call. Complex variable names in Python are usually written with an underscore.
import tkinter as tk
FONT = ('Ariel', 18)
window = tk.Tk()
window.geometry("500x500")
window.title("Stock Watchlist & Logger")
lbl = tk.Label(window, text="Tester", font=FONT)
lbl.pack(padx=20, pady=20)
text_box = tk.Text(window, height=3, font=('Ariel', 16))
text_box.pack(padx=10, pady=10)
num_btn = tk.Frame(window)
yes_no_btn = tk.Frame(window)
num_btn.pack(fill='x')
yes_no_btn.pack(fill='x')
buttons = []
for i in range(1, 7):
btn = tk.Button(num_btn, text=f"{i}", font=FONT)
btn.pack(side="left", fill='x', expand=True)
buttons.append(btn)
yes_btn = tk.Button(yes_no_btn, text="Yes", font=FONT)
yes_btn.pack(side="left", fill='x', expand=True)
no_btn = tk.Button(yes_no_btn, text="No", font=FONT)
no_btn.pack(side="left", fill='x', expand=True)
window.mainloop()
The purpose is to pull a word from the list randomly and display it on the screen in the GUI. The words are displayed in the terminal when I click the button "dammi" but I cannot get them to display in the GUI. I have tried both an Entry and Label with no success.
from tkinter import *
import random
# Window
root = Tk()
root.geometry("400x350")
root.title("Passato Remoto")
root.configure(bg="#000000")
root.resizable(False, False)
# Find Verb
verbi = ['Dare', 'Dire', 'Fare', 'Sapere', 'Prendere']
# Dammi Button
def give():
print(random.choice(verbi))
# Create Buttons
dammi = Button(root, text='Dammi un verbo',
bg='#ffffff',
fg='#000000',
borderwidth=0,
highlightthickness=0,
font=('Helvetica', 16),
height=2,
width=10,
command=give)
dammi.grid(column=0, row=2, pady=50, padx=25)
con = Button(root, text='Coniugazione',
bg='#ffffff',
fg='#000000',
borderwidth=0,
highlightthickness=0,
font=('Helvetica', 16),
height=2,
width=10)
con.grid(column=2, row=2, pady=50, padx=25)
# Put Verb On Screen
verb = Entry(root, text=give(), font=('Helvetica', 40), width=10, bg="#ffffff", fg="#000000")
verb.grid(column=0, columnspan=3, row=1, pady=50, padx=80)
root.mainloop()
The easiest way is probably to use a StringVar. I've added this into the question code.
from tkinter import *
import random
root = Tk()
root.geometry("400x350")
root.title("Passato Remoto")
root.configure(bg="#000000")
root.resizable(False, False)
# Find Verb
verbi = ['Dare', 'Dire', 'Fare', 'Sapere', 'Prendere']
# Dammi Button
sample = StringVar() # Added
def give(): # Changed
sample.set( random.choice(verbi) )
print( sample.get() )
# Create Buttons
dammi = Button(root, text='Dammi un verbo',
bg='#ffffff',
fg='#000000',
borderwidth=0,
highlightthickness=0,
font=('Helvetica', 16),
height=2,
width=10,
command=give)
dammi.grid(column=0, row=2, pady=50, padx=25)
con = Button(root, text='Coniugazione',
bg='#ffffff',
fg='#000000',
borderwidth=0,
highlightthickness=0,
font=('Helvetica', 16),
height=2,
width=10)
con.grid(column=2, row=2, pady=50, padx=25)
# Put Verb On Screen
# changed here
verb = Label(root, font=('Helvetica', 40), textvariable = sample, width=10, bg="#ffffff", fg="#000000")
verb.grid(column=0, columnspan=3, row=1, pady=50, padx=80)
give()
root.mainloop()
I have a problem with a button (Clear) I tried a lot of ways and it did not work and I search about this problem but I did not find the solution to my problem, so I want to clear the contents from the entry when I press on the button Clear.
previous, from the ways I tried it :
entry.delete(0, END)
entry.delete(0, 'end')
entry.insert(index, " ")
and a lot of ways, so if you have the solution give me that, and thank you a lot in advanced
and this is my code:
from tkinter import *
from tkinter import ttk
import pyperclip as clip
root = Tk()
root.geometry('350x500')
root.title("Calculator")
root.configure(bg="#2D3A3E")
root.resizable(False, False)
largeFont = ('Comic Sans MS', 20)
fontButton = ('Comic Sans MS', 20)
style = ttk.Style()
i = 0
def press(x):
global i
i += 1
entry.configure(state=NORMAL)
entry.insert(i, x) # المشكلة هنا أننا عندما نستخدم insert(index, value
entry.configure(state=DISABLED)
def equalResult():
pass
def copyNum():
getText = entry.get()
clip.copy(getText)
clip.paste()
root.update()
def clear():
entry.delete(0, END)
# create buttons and entry
#('winnative', 'clam', 'alt', 'default', 'classic', 'vista', 'xpnative')
entry = Entry(root, textvariable=largeFont, font=largeFont, bg='#2D3A3E', fg="#D6DFE0")
entry.place(width=400, height=100, y=0)
#All Buttons here 19:
style.theme_use('alt')
style.configure("TButton", foreground="#D6DFE0", font=fontButton, background='#2D3A3E')
zero = ttk.Button(root, text='0', command=lambda: press(0))
zero.place(width=75, height=60, y=440, x=0)
dot = ttk.Button(root, text='.', command=lambda: press('.'))
dot.place(width=75, height=60, y=440, x=75)
one = ttk.Button(root, text='1', command=lambda: press(1))
one.place(width=75, height=60, y=380, x=0)
two = ttk.Button(root, text='2', command=lambda: press(2))
two.place(width=75, height=60, y=380, x=75)
three = ttk.Button(root, text='3', command=lambda: press(3))
three.place(width=75, height=60, y=380, x=150)
four = ttk.Button(root, text='4', command=lambda: press(4))
four.place(width=75, height=60, y=320, x=0)
five = ttk.Button(root, text='5', command=lambda: press(5))
five.place(width=75, height=60, y=320, x=75)
six = ttk.Button(root, text='6', command=lambda: press(6))
six.place(width=75, height=60, y=320, x=150)
seven = ttk.Button(root, text='7', command=lambda: press(7))
seven.place(width=75, height=60, y=260, x=0)
eight = ttk.Button(root, text='8', command=lambda: press(8))
eight.place(width=75, height=60, y=260, x=75)
nine = ttk.Button(root, text='9', command=lambda: press(9))
nine.place(width=75, height=60, y=260, x=150)
equal = Button(root, text='=', fg="#D6DFE0", bg="green", font=("Comic Sans MS", 30, 'bold'),
command=lambda: press('='))
equal.place(width=200, height=60, y=440, x=150)
plus = Button(root, text='+', fg="#D6DFE0", bg="green", font=("Comic Sans MS", 30,'bold'),
command=lambda: press('+'))
plus.place(width=125, height=60, y=380, x=225)
minus = Button(root, text='ـــ', fg="#D6DFE0", bg="green", font=("arial", 30,'bold'),
command=lambda: press('-'))
minus.place(width=125, height=60, y=320, x=225)
multiply = Button(root, text='x', fg="#D6DFE0", bg="green", font=("arial", 30, 'bold'),
command=lambda: press('*'))
multiply.place(width=125, height=60, y=260, x=225)
mod = ttk.Button(root, text='%', command=lambda: press('%'))
mod.place(width=75, height=60, y=200, x=150)
# btn clear all text in the entry
Clear = ttk.Button(root, text='C', command=lambda: clear())
Clear.place(width=75, height=60, y=200, x=75)
# button remove text from the end
remove = ttk.Button(root, text='rem')
remove.place(width=75, height=60, y=200, x=0)
divide = Button(root, text='/', fg="#D6DFE0", bg="green", font=("arial", 30,'bold'),
command=lambda: press('/'))
divide.place(width=125, height=60, y=200, x=225)
# btn color mode
colorMode = Button(root, text='color mode', fg="#D6DFE0", bg="orange", font=("arial",
25,'bold'))
colorMode.place(width=225, height=40, y=160, x=1)
# btn copy
Copy = Button(root, text='copy', fg="#D6DFE0", bg="GREEN", font=("arial", 15,'bold'),
command=lambda: copyNum())
Copy.place(width=125, height=40, y=160, x=225)
entry.configure(state=DISABLED)
mainloop()
You aren't re enabling that entry. You had disabled that entry from here and not enabling. So, you are facing this issue:
def clear():
entry.configure(state=NORMAL)
entry.delete(0, END)
entry.configure(state=DISABLED)
Doing this should solve your problem.
And additionally you don't have to use other library just to copy text from entry, you can use this:
def copy_button():
clip = Tk()
clip.withdraw()
clip.clipboard_clear()
entry.configure(state=NORMAL)
text=entry.get()
entry.configure(state=DISABLED)
clip.clipboard_append(text)
clip.destroy()
I'm new to tkinter and i've built this so far:
import tkinter as tk
import subprocess
import os
def maingui():
window = tk.Tk()
window.title("My first GUI")
#window.state('zoomed')
window.geometry("700x205")
window.configure(bg="black")
frame1 = tk.Frame(window, width=90, bg="orange")
# frame1.pack(fill=tk.Y, side=tk.LEFT)
frame1.grid(row=0, column=0)
frame2 = tk.Frame(window, width=1890, bg="black")
# frame2.pack(fill=tk.Y, side=tk.RIGHT)
frame2.grid(row=0, column=1)
lotomat = tk.Button(frame1, text=" Start\n Lotomat", padx=10, pady=5, bg="orange", fg="black",
relief=tk.GROOVE, command=lambda : startLotomat())
# lotomat.pack()
lotomat.grid(row=1, column=0)
convert = tk.Button(frame1, text=" URL2IP \n on \n Desktop", padx=10, pady=5, bg="orange", fg="black",
relief=tk.GROOVE, command=lambda : startURL2IP())
# convert.pack()
convert.grid(row=2, column=0)
startRps = tk.Button(frame1, text=" Start \nR.P.S", padx=12, pady=5, bg="orange", fg="black",
relief=tk.GROOVE, command=lambda : startRPS())
# startRps.pack()
startRps.grid(row=3, column=0)
endRun = tk.Button(frame1, text="Quit", padx=12, pady=10, bg="orange", fg="black",
relief=tk.RIDGE, command=lambda : ending())
# endRun.pack()
endRun.grid(row=4, column=0)
def startLotomat():
os.system('python lotomat.py')
def startURL2IP():
os.system('python urltoipondesktop.py')
def startRPS():
os.system('python rockpaperscissors.py')
def ending():
exit()
window.mainloop()
maingui()
each button runs a different .py file.
how can I use frames to split the window so the program runs on the right side?
Thanks!
Edit:
I've added a pic of the GUI, the goal is to run the left menu buttons on the black frame.
Following acw1668's comment and alot of thread researching here's the thread solution. so elegant, so simple! I love python!
def thread_handler(self, host):
wt = threading.Thread(target=self.write_to_file, args=(host,))
pt = threading.Thread(target=self.print_to_box, args=(host,))
dt = threading.Thread(target=self.delete_default_lines, args=())
wt.start()
pt.start()
dt.start()
I'm pretty much a complete beginner when it comes to coding. I have been using python and the tkinter GUI to code a booking system. Eventually the database will need to be integrated but I'm trying to set up the GUI first. My code is a little messy but I'm not worried about that at the minute.
I'm try to get a button infront of the frame but it stays behind, any way of stopping this?
Here's my code for this...
def home_button():
home_title = Button(book, text='HOME', background='#DEEBF7', activebackground='#DEEBF7', width=15, border=0, font = ("Helvetica 15 italic"))
home_title.place(x=423, y=81)
def bookings_button():
bookings_title = Label(book, text='BOOKINGS', background='#DEEBF7', font = ("Helvetica 15 italic"))
bookings_title.place(x=475, y=85)
new_booking = Button(book, text='NEW BOOKING', width=20, height=2, background='#87B7E2')
new_booking.place(x=160, y=170)
def students_button():
students_title = Label(book, text='STUDENTS', background='#DEEBF7', font = ("Helvetica 15 italic"))
students_title.place(x=475, y=85)
## GUI ####
from tkinter import *
root = Tk()
#create the root window
book = Frame(root)
book.grid()
root.title("Home")
root.geometry("850x550")
root.configure(background='#DEEBF7')
backg = Button(book, height=850, width=550, background='#DEEBF7',
activebackground='#DEEBF7', border=0)
backg.pack()
#modify the windows size, colour and the size of the grid
logo1 = Button(book, width=175, height=117, border=0, background='#DEEBF7', activebackground='#DEEBF7', command=home_button)
logo = PhotoImage(file='Logo1.gif')
logo1.image = logo
logo1.configure(image=logo)
logo1.place(x=50, y=20)
title = Label(book, text = "GRAHAM'S SCHOOL OF MOTORING", background = '#DEEBF7', font = ("Helvetica 24 bold"))
title.place(x=250, y=40)
frame=Frame(root, width=551, height=384, background='#000000')
frame.place(x=250, y=135)
frame=Frame(root, width=547, height=380, background='#DEEBF7', borderwidth=5)
frame.place(x=252, y=137)
home = Button(book, text = "HOME", height=2, width=20, background='#5F85CD',
borderwidth=5, activebackground='#A2C7E8', relief='raised', command=home_button)
home.place(x=60, y=150)
bookings = Button(book, text="BOOKINGS", height=2, width=20, background='#5F85CD', borderwidth=5, activebackground='#A2C7E8', relief='raised', command=bookings_button)
bookings.place(x=60, y=235)
student = Button(book, text="STUDENTS", height=2, width=20, background='#5F85CD',
borderwidth=5, activebackground='#A2C7E8', relief='raised', command=students_button)
student.place(x=60, y=320)
back = Button(book, text="BACK", height=2, width=20, background='#FF5559',
borderwidth=5, activebackground='#BF230A', relief='raised')
back.place(x=45, y=450)
root.mainloop()
#kick off the window's event-loop
To put the button on top of the frame, I changed the master widget of the button:
I replaced
new_booking = Button(book, text='NEW BOOKING', width=20, height=2, background='#87B7E2')
by
new_booking = Button(root, text='NEW BOOKING', width=20, height=2, background='#87B7E2')