from tkinter import *
import time
check = False
window = Tk()
window.geometry("1920x1080")
def typeTime():
hour = int(time.strftime("%H"))
minute = int(time.strftime("%M"))
second = int(time.strftime("%S"))
hourInput2 = int(hourInput.get())
minuteInput2 = int(minuteInput.get())
secondInput2 = int(secondInput.get())
if(hour == hourInput2 and minute == minuteInput2 and second == secondInput2):
print("now")
global check
check = True
canvas = Canvas(window, width = 1980, height = 1020)
canvas.pack()
hourInput = StringVar()
minuteInput = StringVar()
secondInput = StringVar()
setHour = Entry(window, text = hourInput, font = (20)).place(x = 100, y = 20, width = 100, height = 40)
setMinute = Entry(window, text = minuteInput, font = (20)).place(x = 300, y = 20, width = 100, height = 40)
setSecond = Entry(window, text = secondInput, font = (20)).place(x = 500, y = 20, width = 100, height = 40)
canvas.create_text(60, 40, text = "Hour: ", font = (20))
canvas.create_text(260, 40, text = "Minute: ", font = (20))
canvas.create_text(460, 40, text = "Second: ", font = (20))
submit = Button(text = "Submit", height = 2, width = 10, font = (10), command = typeTime)
submit.place(x = 100, y = 100)
if check == True:
print("Pressed")
submit.config(relief = SUNKEN)
window.mainloop()
I'm trying to make a button to stay pressed, so I tried to make this happens with a global variable. The variable check is initially False, but when typeTime() is called via the submit object it should change its value in True and when check will be tested later to keep my button pressed using config method.
What am I doing wrong, as neither the button is still pressed nor the message "Pressed" is displayed in the console ?
The window.mainloop() is the internal loop inside object window, not in your script so that is why it didn't work. You need to add the action inside the function typeTime:
from tkinter import *
import time
if __name__=='__main__':
check = False
window = Tk()
window.geometry("1920x1080")
def typeTime(button):
hour = int(time.strftime("%H"))
minute = int(time.strftime("%M"))
second = int(time.strftime("%S"))
hourInput2 = int(hourInput.get())
minuteInput2 = int(minuteInput.get())
secondInput2 = int(secondInput.get())
if(hour == hourInput2 and minute == minuteInput2 and second == secondInput2):
print("now")
# global check
# check = True
print('Pressed')
button.config(relief=SUNKEN)
canvas = Canvas(window, width = 1980, height = 1020)
canvas.pack()
hourInput = StringVar()
minuteInput = StringVar()
secondInput = StringVar()
setHour = Entry(window, text = hourInput, font = (20)).place(x = 100, y = 20, width = 100, height = 40)
setMinute = Entry(window, text = minuteInput, font = (20)).place(x = 300, y = 20, width = 100, height = 40)
setSecond = Entry(window, text = secondInput, font = (20)).place(x = 500, y = 20, width = 100, height = 40)
canvas.create_text(60, 40, text = "Hour: ", font = (20))
canvas.create_text(260, 40, text = "Minute: ", font = (20))
canvas.create_text(460, 40, text = "Second: ", font = (20))
submit = Button(text = "Submit", height = 2, width = 10, font = (10))
submit.config(command = lambda submit=submit:typeTime(submit))
submit.place(x = 100, y = 100)
# if check == True:
# print("Pressed")
# submit.config(relief = SUNKEN)
window.mainloop()
Related
In my app, settings button are initiating a window, in that window you insert your link and this link need to get back to main function and work with button, which open this link in browser
from tkinter import *
from tkinter import messagebox
def validate(P):
if len(P) == 0:
return True
elif len(P) <= 10:
return True
else:
return False
def validate_en(s):
pass
def close():
if messagebox.askokcancel("Close window", "Are u sure you want to exit?"):
web.destroy()
if __name__ == "__main__":
web = Tk()
web.configure(background = "black")
web.title("WebSaver")
web.geometry("600x400")
web.iconbitmap('icon.ico')
web.resizable(False, False)
web.protocol("WM_DELETE_WINDOW", close)
def main_content():
web.withdraw()
main = Toplevel(web)
main.title("Application")
main.geometry("800x600")
main.iconbitmap('icon.ico')
main.protocol("WM_DELETE_WINDOW", close)
main.resizable(False, False)
canvas = Canvas(main,width = "800", height = "600", highlightthickness = 0, bg =
"black")
canvas.pack(fill = BOTH, expand = True)
upper_text = Label(canvas, text= "TEXT", justify = "center", background =
"black", foreground = "white", font = "KacstDigital, 20", borderwidth = 2,
highlightthickness = 1, highlightbackground = "gray")
upper_text.pack(padx = 5)
def link_1():
pass
button_1 = Button(canvas, text = "First link", bg = "#293133", fg = "white", font
= "KacstDigital, 18", height = 2, width = 15, command = lambda: link_1)
button_1.place(x = 30, y = 150)
button_conf_1 = Button(canvas, text = "settings", bg = "#293133", fg = "white",
font = "KacstDigital, 7", width = 7, command = configurate_button_1)
button_conf_1.place(x = 198, y = 203)
def configurate_button_1():
def close():
conf.destroy()
def apply(link):
if (link.isspace() or link == ""):
messagebox.showwarning(title = "Error!", message = "Input is null")
else:
close()
return link
conf = Toplevel(web)
conf.title("Application")
conf.geometry("600x100")
conf.iconbitmap('icon.ico')
conf.protocol("WM_DELETE_WINDOW", close)
conf.resizable(False, False)
canvas = Canvas(conf, width = "600", height = "100", highlightthickness = 0, bg =
"black")
canvas.pack(fill = BOTH, expand = True)
vcmd = (canvas.register(validate_en), '%s')
link_text = Label(canvas, text= "Link: ", justify = "center", background =
"black", foreground = "white", font = "KacstDigital, 12", borderwidth = 2,
highlightthickness = 1, highlightbackground = "gray")
link_text.place(x = 5, y = 10)
link_entry = Entry(canvas, bg = "#293133", fg = "white", font = "KacstDigital,
14", width = 45, validate = "key", validatecommand = vcmd)
link_entry.place(x = 78, y = 10)
link_button = Button(canvas, text = "Confirm changes", bg = "#293133", fg =
"white", font = "KacstDigital, 14", height = 1, width = 20, command = lambda:
apply(link_entry.get()))
link_button.place(x = 15, y = 50)
main_content()
web.mainloop()
I need help with getting value from link_entry (in conf.button func.) and give this value to main_content to open this link from main_content.
Sorry for this strange option strings, idk how to get them at the right place
from tkinter import *
import time
def checkTime():
if len(hourInput.get()) != 0 and len(minuteInput.get()) != 0 and len(secondInput.get()) != 0:
if hourInput.get() == time.strftime("%H"):
print("good")
window.after(500, checkTime)
def pressButton(button):
button.config(relief=SUNKEN)
if __name__=='__main__':
window = Tk()
window.geometry("1920x1080")
canvas = Canvas(window, width = 1980, height = 1020)
canvas.pack()
hourInput = StringVar()
minuteInput = StringVar()
secondInput = StringVar()
setHour = Entry(window, text = hourInput, font = (20))
setHour.place(x = 100, y = 20, width = 100, height = 40)
setMinute = Entry(window, text = minuteInput, font = (20))
setMinute.place(x = 300, y = 20, width = 100, height = 40)
setSecond = Entry(window, text = secondInput, font = (20))
setSecond.place(x = 500, y = 20, width = 100, height = 40)
canvas.create_text(60, 40, text = "Hour: ", font = (20))
canvas.create_text(260, 40, text = "Minute: ", font = (20))
canvas.create_text(460, 40, text = "Second: ", font = (20))
submit = Button(text = "Submit", height = 2, width = 10, font = (10))
submit.config(command = lambda submit=submit:pressButton(submit))
submit.place(x = 100, y = 100)
checkTime()
window.mainloop()
I want the function checkTime() to be called when my button is pressed. But how to get the status of my button and compare it ? I want to use the function only if the button is pressed as a test that the user agree with his inputs
You can modify the button declaration as follows so that the checkTime() will trigger when the button is pressed.
submit = Button(text = "Submit", height = 2, width = 10, font = (10), relief=SUNKEN)
submit['command'] = checkTime # no parentheses here
Also make sure that the checkTime() method call in the bottom is removed
I put the function checkTime() inside the pressButton() function, and now the program works fine.
from tkinter import *
import time
def checkTime():
if len(hourInput.get()) != 0 and len(minuteInput.get()) != 0 and len(secondInput.get()) != 0:
if hourInput.get() == time.strftime("%H"):
print("good")
window.after(500, checkTime)
def pressButton(button):
button.config(relief = SUNKEN)
checkTime()
if __name__== '__main__':
window = Tk()
window.geometry("1920x1080")
canvas = Canvas(window, width = 1980, height = 1020)
canvas.pack()
hourInput = StringVar()
minuteInput = StringVar()
secondInput = StringVar()
setHour = Entry(window, text = hourInput, font = (20))
setHour.place(x = 100, y = 20, width = 100, height = 40)
setMinute = Entry(window, text = minuteInput, font = (20))
setMinute.place(x = 300, y = 20, width = 100, height = 40)
setSecond = Entry(window, text = secondInput, font = (20))
setSecond.place(x = 500, y = 20, width = 100, height = 40)
canvas.create_text(60, 40, text = "Hour: ", font = (20))
canvas.create_text(260, 40, text = "Minute: ", font = (20))
canvas.create_text(460, 40, text = "Second: ", font = (20))
submit = Button(text = "Submit", height = 2, width = 10, font = (10))
submit.config(command = lambda submit=submit:pressButton(submit))
submit.place(x = 100, y = 100)
window.mainloop()
The following code gets an error as it says name 'Combo_Box Value' is not defined.
from tkinter import *
from tkinter.ttk import Progressbar
from tkinter.ttk import Combobox
from tkinter.ttk import Notebook
import tkinter.font
class Pounds_Converter():
def __init__(self, parent):
self.gui(parent)
def gui(self, parent):
if parent == 0:
self.w1 = Tk()
self.w1.configure(bg = '#e4f3ff')
self.w1.geometry('370x350')
else:
self.w1 = Frame(parent)
self.w1.configure(bg = '#e4f3ff')
self.w1.place(x = 0, y = 0, width = 370, height = 350)
self.label3 = Label(self.w1, text = "Pounds Converter", bg = "#e4f3ff", fg = "#002d5e", font = tkinter.font.Font(family = "Myanmar Text", size = 24), cursor = "arrow", state = "normal")
self.label3.place(x = 55, y = 50, width = 260, height = 62)
self.label4 = Label(self.w1, text = "Pounds", bg = "#e4f3ff", fg = "#006fe8", font = tkinter.font.Font(family = "Myanmar Text", size = 12), cursor = "arrow", state = "normal")
self.label4.place(x = 60, y = 135, width = 60, height = 22)
self.combo1 = Combobox(self.w1, font = tkinter.font.Font(family = "Myanmar Text", size = 12), cursor = "arrow", state = "normal")
self.combo1.place(x = 250, y = 135, width = 76, height = 22)
self.combo1['values'] = ("Dollars", "Euros")
self.combo1.current(0)
self.combo1.bind("<<ComboboxSelected>>", self.Combo_Box)
self.ltext3 = Entry(self.w1, bg = "#efffff", font = tkinter.font.Font(family = "MS Shell Dlg 2", size = 8), cursor = "arrow", state = "normal")
self.ltext3.place(x = 20, y = 170, width = 140, height = 32)
self.ltext3.insert(INSERT, "0.00")
self.ltext4 = Entry(self.w1, bg = "#efffff", font = tkinter.font.Font(family = "MS Shell Dlg 2", size = 8), cursor = "arrow", state = "normal")
self.ltext4.place(x = 220, y = 170, width = 140, height = 32)
self.button2 = Button(self.w1, text = "Convert", bg = "#efffff", fg = "#006fe8", font = tkinter.font.Font(family = "MS Shell Dlg 2", size = 8), cursor = "arrow", state = "normal")
self.button2.place(x = 120, y = 270, width = 130, height = 30)
self.button2['command'] = self.Convert
self.button3 = Button(self.w1, text = "Home", bg = "#e4f3ff", fg = "#006fe8", font = tkinter.font.Font(family = "Myanmar Text", size = 8), cursor = "arrow", state = "normal")
self.button3.place(x = 20, y = 20, width = 40, height = 16)
def Combo_Box(self, e):
Combo_Box_Value=self.combo1.get()
return Combo_Box_Value
def Convert(self):
global Combo_Box_Value
Pounds=self.ltext3.get()
Pounds = float(Pounds)
if Combo_Box_Value== "Dollars":
Dollars=Pounds*1.38
self.ltext4.delete("0",END)
self.ltext4.insert(INSERT,Dollars)
a = Pounds_Converter(0)
a.w1.mainloop()
That is the full code for that GUI widget. I would like to get the value of the combo box once it is changed then I need to use it in another variable.
I am trying to make a game with Python, but I ask a question because it is difficult to make.
The question is, when I press the button join, I check if the value of entry is duplicated and if not, I save it in a file called db.txt. I get an error. What should I do?
the fruit of my efforts
import tkinter
root = tkinter.Tk()
root.title("game")
root.geometry("1000x700")
SPSP = 100
index = 0
btn_c = 0
mouse_x = 0
mouse_y = 0
bg2 = tkinter.PhotoImage(file = "C:\\Users\\user\\Pictures\\Defense game\\Map.png")
bg = tkinter.PhotoImage(file = "C:\\Users\\user\\Pictures\\Defense game\\game1.png")
bg3 = tkinter.PhotoImage(file = "C:\\Users\\user\\Pictures\\Defense game\\Login screen.png")
def click():
global bg, bg2, index, btn_c
canvas2 = tkinter.Canvas(root, width = 1000, height = 600)
canvas2.pack()
index = 1
if index == 1:
bg2 = tkinter.PhotoImage(file = "C:\\Users\\user\\Pictures\\Defense game\\Map.png")
canvas2.create_image(500, 300, image = bg2)
canvas2.place(x = 0, y = 0)
def login():
global bg, bg2, bg3, index, btn_c
canvas3 = tkinter.Canvas(root, width = 1000, height = 600)
canvas3.pack()
index = 2
if index == 2:
bg3 = tkinter.PhotoImage(file = "C:\\Users\\user\\Pictures\\Defense game\\Login screen.png")
canvas3.create_image(500, 300, image = bg3)
canvas3.place(x = 0, y = 0)
label4 = tkinter.Label(text = "ID", font = ("정서체", 30))
label4.place(x = 10, y = 200)
label4 = tkinter.Label(text = "pw", font = ("정서체", 30))
label4.place(x = 10, y = 300)
entry = tkinter.Entry(root, width = 20)
entry.insert(0, "More than 3 characters and less than 20 characters")
def db_join():
save = 0
global data_ID, data_pw
if len(entry.get()) > 3 and len(entry.get()) <20 and len(entry2.get()) > 5 and len(entry2.get()) <20:
try:
with open("db.txt") as user_db:
entry_text = entry.get()
entry2_text = entry2.get()
for line in user_db:
line = line.strip().split()
if line[0] == entry_text:
save = 1
break
except FileNotFoundError:
pass
if save == 0:
f = open("db.txt", "a")
data_ID = entry.get()
data_pw = entry2.get()
f.write(data_ID + " " + data_pw + "\n")
f.close()
def clear(event):
if entry.get() == "More than 3 characters and less than 20 characters":
entry.delete(0, len(entry.get()))
def clear2(event):
if entry2.get() == "More than 5 characters and less than 20 characters":
entry2.delete(0, len(entry2.get()))
entry.bind("<Button-1>", clear)
entry.place(x = 40, y = 250)
entry2 = tkinter.Entry(root, width = 20)
entry2.config(show = "*")
entry2.insert(0, "More than 5 characters and less than 20 characters")
entry2.bind("<Button-1>", clear2)
entry2.place(x = 40, y = 350)
btn_Join = tkinter.Button(text = "join membership", font = ("굴림체", 50), fg = "skyblue", command = db_join)
btn_Join.place(x = 300, y = 400)
btn_login = tkinter.Button(text = "Login", font = ("굴림체", 50), fg = "skyblue")
btn_login.place(x = 300, y = 250)
def game_main():
global bg, bg2, index, btn_c
canvas = tkinter.Canvas(root, width = 1000, height = 600)
canvas.pack()
if index == 0:
bg = tkinter.PhotoImage(file = "C:\\Users\\user\\Pictures\\Defense game\\game1.png")
canvas.create_image(500, 300, image = bg)
btn = tkinter.Button(text = "start", font = ("굴림체",20), command = click)
btn.place(x = 460, y = 600)
btn2 = tkinter.Button(text = "join membership", font = ("굴림체", 20), command = login)
btn2.place(x = 550, y = 600)
my_base = [
[0,0],
[0,0],
[0,0],
[0,0],
[0,0],
[0,0],
]
your_base = [
[1,1],
[1,1],
[1,1],
[1,1],
[1,1],
[1,1],
]
game_main()
root.mainloop()
These are my codes.
What I want is to check if the entry value is duplicated here, and if it is not duplicated, put it in the txt file and cancel it if it is duplicated
EXE won't open. I have tried everything I found online.
When I run the exe the console opens for a few seconds and then closes.
How can I make this .py executable and have my .txt output?
from tkinter import *
import time, os, fnmatch, shutil
screen = Tk()
screen.geometry("600x450")
screen.configure(background='azure')
screen.title("M")
t = time.localtime()
timestamp = time.strftime('%b-%d-%Y_%H%M', t)
BACKUP_NAME = ("backup-" + timestamp)
heading = Label(text = "Mr", font=(None, 15), bg = "lightblue", fg = "black", width = "500", height = "3")
heading.pack()
firstname_text = Label(text = "Name/ * ", bg = "azure",)
lastname_text = Label(text = "Usagel* ", bg = "azure",)
material_text = Label(text = "M * ", bg = "azure",)
age_text = Label(text = "How * ", bg = "azure",)
firstname_text.place(x = 15, y = 80)
lastname_text.place(x = 15, y = 140)
material_text.place(x = 15, y = 210)
age_text.place(x = 15, y = 260)
firstname = StringVar()
lastname = StringVar()
material = StringVar()
age = StringVar()
firstname_entry = Entry(textvariable = firstname, width = "30")
lastname_entry = Entry(textvariable = lastname, width = "30")
material_entry = Entry(textvariable = material, width = "30")
age_entry = Entry(textvariable = age, width = "30")
firstname_entry.place(x = 15, y = 100)
lastname_entry.place(x = 15, y = 170)
material_entry.place(x = 15, y = 240)
age_entry.place(x = 15, y = 290)
register = Button(screen,text = "Submit", font=(None, 10), width = "30", height = "2", command = save_info, bg = "lime green")
register.place(x = 15, y = 320)