Related
`I am creating a loging page where user needs to choose color(objekta krāsa) for car. User need to chose from
blue(Zilā krāsa) and black(melnā krāsa) colors in the drop down menu and when button "Ienākt" is clicked it starts shoud start drawing a car with selected,but now when i pres ienākt it shows blank page and also when both drop down menus are emty its still works and doesnt show error. I been trying to fix this for all evening, and i still learning.How to get that when users choses color and clicks ienākt buttom drawing starts at he chosen color?and when all the menus are emty it should not work.
i would appreciate your help!
from tkinter import *
from tkinter import messagebox
import tkinter as tk
from tkinter import ttk
import turtle
root = Tk()
root .title('Ienākt')
root .geometry('925x500+300+200')
root. configure(bg="#fff")
root .resizable(False, False)
img = PhotoImage(file=r'C:\Users\Inga\Downloads\ienakt.png')
Label(root, image=img, bg='white').place(x=5, y=50)
frame = Frame(root, width=350, height=400, bg="white")
frame. place(x=480, y=70)
heading = Label(frame, text='Ienākt', fg='#57a1f8', bg='white',
font=('Microsoft YaHei UI Light', 23, 'bold'))
heading.place(x=100, y=5)
# -----
def on_enter(e):
user.delete(0, 'end')
def on_leave(e):
name = user.get()
if name == '':
user.insert(0, 'E-pasts')
user = Entry(frame, width=25, fg='#000000', border=0,
bg="white", font=('Microsoft YaHei UI Light', 11))
user.place(x=30, y=80)
user.insert(0, 'E-pasts')
user.bind('<FocusIn>', on_enter)
user.bind('<FocusOut>', on_leave)
Frame(frame, width=295, height=2, bg='#000000').place(x=25, y=107)
# -----
def on_enter(e):
password.delete(0, 'end')
def on_leave(e):
name = password.get()
if name == '':
password.insert(0, 'Parole')
password = Entry(frame, width=25, fg='#000000', border=0,
bg="white", font=('Microsoft YaHei UI Light', 11))
password.place(x=30, y=150)
password.insert(0, 'Parole')
password.bind('<FocusIn>', on_enter)
password.bind('<FocusOut>', on_leave)
Frame(frame, width=295, height=2, bg='#000000').place(x=25, y=177)
# ----tekts krasa
label = Label(root, text="Objekta krāsa", width=41,
font=("Microsoft YaHei UI Light", 11), bg='white')
label.place(x=467, y=291)
# ---IZVĒLE-krāsa
col = tk.StringVar()
col_chs = ttk.Combobox(frame, width=45, textvariable=col, state='readonly')
col_chs['values'] = ('Melnā krāsa', 'Zilā krāsa')
col_chs.place(x=25, y=249)
Frame(frame, width=295, height=2, bg='#000000').place(x=25, y=247)
# ----objekts
label = Label(root, text="Objekts", width=41,
font=("Microsoft YaHei UI Light", 11), bg='white')
label.place(x=467, y=361)
# ----Izvēle-- objekts
col = tk.StringVar()
col_chs = ttk.Combobox(frame, width=45, textvariable=col, state='readonly')
col_chs['values'] = ('Mašīna',)
col_chs.place(x=25, y=318)
Frame(frame, width=295, height=2, bg='#000000').place(x=25, y=317)
# ----
def draw_object():
screen = turtle.Screen()
color = col_chs.get()
car = turtle.Turtle()
if color == 'Melnā krāsa':
car.color("black")
car.fillcolor("black")
car.begin_fill()
car.forward(370)
car.left(90)
car.forward(50)
car.left(90)
car.forward(370)
car.left(90)
car.forward(50)
car.end_fill()
# Below code for drawing window and roof
car.penup()
car.goto(100, 50)
car.pendown()
car.setheading(45)
car.forward(70)
car.setheading(0)
car.forward(100)
car.setheading(-45)
car.forward(70)
car.setheading(90)
car.penup()
car.goto(200, 50)
car.pendown()
car.forward(49.50)
car.penup()
car.goto(100, -10)
car.pendown()
car.begin_fill()
car.circle(20)
car.end_fill()
car.penup()
car.goto(300, -10)
car.pendown()
car.begin_fill()
car.circle(20)
car.end_fill()
turtle.exitonclick()
elif color == 'Zilā krāsa':
car.color("blue")
car.fillcolor("blue")
car.penup()
car.goto(0, 0)
car.pendown()
car.begin_fill()
car.forward(370)
car.left(90)
car.forward(50)
car.left(90)
car.forward(370)
car.left(90)
car.forward(50)
car.end_fill()
# Below code for drawing window and roof
car.penup()
car.goto(100, 50)
car.pendown()
car.setheading(45)
car.forward(70)
car.setheading(0)
car.forward(100)
car.setheading(-45)
car.forward(70)
car.setheading(90)
car.penup()
car.goto(200, 50)
car.pendown()
car.forward(49.50)
car.penup()
car.goto(100, -10)
car.pendown()
car.begin_fill()
car.circle(20)
car.end_fill()
car.penup()
car.goto(300, -10)
car.pendown()
car.begin_fill()
car.circle(20)
car.end_fill()
turtle.exitonclick()
# ---
def login():
user_name = user.get()
user_password = password.get()
if user_name == "E-pasts" or user_password == "Parole":
messagebox.showerror("Kļūda", "Aizpildiet visus laukus")
else:
if user_name == "admin" and user_password == "admin":
messagebox.showinfo("Paziņojums", "Ienācāt")
draw_object()
turtle.mainloop()
else:
messagebox.showerror("Kļūda", "Nepareizs e-pasts vai parole")
# ---- ieiešanas poga
login_button = Button(frame, width=41, pady=7,
text='Ienākt', bg='#57a1f8', fg='white', command=draw_object)
login_button["command"] = draw_object
login_button.place(x=23, y=360)
def open_login_page():
exec(open("path/to/login_page_code.py").read())
root.mainloop()
`
At first i tried to make when button is clicked it call the draw function and then draw and then i added if and elif in the draw_object and try to change some little things but that didnt help.
i would appreciate your help!
I'm creating this program that it's not complete yet, a small detail that annoys me, when you run the program and click "OPEN" it creates a new Toplevel window inside the def click_selettore function in the beginning, and i notice that the black screen on the left "blinks" for a split second, for a split second is white and then it become black i hope you notice this, the photo attached it's a frame of the Toplevel opening window and i notice the white frames that are loading i think, and they are white for a split second. How to solve this and not making the new window white for the split second? Thanks, here is the code it's a bit long but the probelm is only the white blinks when creating the window inside the "click selettore" function, if you help me i would really appreciate it
from tkinter import *
import customtkinter
from ctypes import windll
windll.shcore.SetProcessDpiAwareness(1)
def click_selettore(event):
bg = "#414141"
win.attributes('-topmost', 0)
win2 = Toplevel(win)
win2.focus()
win2.resizable(False,False)
win2.grab_set()
win2.configure(bg=bg)
win2.title("Selettore colori")
frame_schermo = Frame(win2)
frame_schermo.grid(row=0,column=0,padx=(20,0),pady=(20,0))
schermo = Label(frame_schermo, bg="#000000", width=45, height=9, relief="solid", borderwidth=3)
schermo.grid(row=0,column=0)
frame_slider = Frame(win2,bg=bg)
frame_slider.grid(row=0,column=1,sticky=N,pady=(20,0),padx=(50,0))
etc_rosso = Label(frame_slider, text="Rosso", bg=bg, fg="#C70000", font=("Ink Free", 20, "bold"))
etc_rosso.grid(row=0,column=0,pady=(11,0))
slider_rosso = customtkinter.CTkSlider(frame_slider, from_=0, to=255,width=230,progress_color='#C70000',fg_color='#a34949',button_hover_color='#3f9feb',button_color='#1f82d1')
slider_rosso.grid(row=0,column=1,sticky=N,padx=(10,0),pady=(23,0))
slider_rosso.set(0)
casella_rosso = customtkinter.CTkEntry(master=frame_slider,text_font=('Courier',15,'bold'),width=55)
casella_rosso.grid(row=0,column=2,pady=(11,0),padx=(7,20))
casella_rosso.insert(0,0)
etc_verde = Label(frame_slider, text="Verde", bg=bg, fg="#3fc441", font=("Ink Free", 20, "bold"))
etc_verde.grid(row=1,column=0,pady=(11,0))
slider_verde = customtkinter.CTkSlider(frame_slider, from_=0, to=255, width=230, progress_color='#3fc441', fg_color='#3d803e', button_hover_color='#3f9feb', button_color='#1f82d1')
slider_verde.grid(row=1, column=1, sticky=N,padx=(10,0),pady=(23,0))
slider_verde.set(0)
casella_verde = customtkinter.CTkEntry(master=frame_slider, text_font=('Courier', 15, 'bold'), width=55)
casella_verde.grid(row=1, column=2,pady=(11,0),padx=(7,20))
casella_verde.insert(0, 0)
etc_blu = Label(frame_slider, text="Blu", bg=bg, fg="#010178", font=("Ink Free", 20, "bold"))
etc_blu.grid(row=2,column=0,pady=(11,0))
slider_blu = customtkinter.CTkSlider(frame_slider, from_=0, to=255, width=230, progress_color='#010178',fg_color='#222275', button_hover_color='#3f9feb', button_color='#1f82d1')
slider_blu.grid(row=2, column=1, sticky=N,padx=(10,0),pady=(23,0))
slider_blu.set(0)
casella_blu = customtkinter.CTkEntry(master=frame_slider, text_font=('Courier', 15, 'bold'), width=55)
casella_blu.grid(row=2, column=2,pady=(11,0),padx=(7,20))
casella_blu.insert(0, 0)
frame_sotto_slider = Frame(win2,bg=bg)
frame_sotto_slider.grid(row=1,column=0,pady=(45,0),padx=(35,0))
etc_esadecimale_titolo= Label(frame_sotto_slider,text='Esadecimale -> ',font=('Courier',16),fg='white',bg=bg)
etc_esadecimale_titolo.grid(row=0,column=0)
campo_esadecimale = customtkinter.CTkEntry(master=frame_sotto_slider,text_font=('Courier',16),width=105)
campo_esadecimale.grid(row=0,column=1,padx=(6,0))
btn_ok = customtkinter.CTkButton(win2,text='Ok',text_font=('Courier',16),width=50,corner_radius=10,hover_color='#3a76a6')
btn_ok.grid(row=3,column=1,sticky=E,pady=(50,10),padx=(0,10))
win.eval(f'tk::PlaceWindow {str(win2)} center')
win2.mainloop()
win = Tk()
win.configure(bg="#2b2b2b")
etichetta_selettore_colori = Label(win,text='OPEN',relief="solid",borderwidth=4,font=('Helvetica',17))
etichetta_selettore_colori.grid(row=0,column=0,padx=(20,0),pady=20)
etichetta_selettore_colori.bind("<Button-1>",click_selettore)
win.eval('tk::PlaceWindow . center')
win.mainloop()
i am currently making an app which displays information about NBA players using an API called data.nba.net, i am trying to display information such as a player's last affiliation, height, jersey number, etc. But at some point in the process the labels cut off and it stops showing the rest of them.
I've tried messing around with the dimensions of both the window and the canvas, but it doesn't seem to solve the problem. I am not an expert on tkinter, so i am not sure how do canvases and frames work, pls help :,).
What it should look like
What it looks like at the end
from tkinter import *
from turtle import position
from weakref import WeakSet
import nba_now
from nba_now import *
from tkinter import ttk
main_window = Tk()
main_window.geometry("420x420")
main_window.title("NBA NOW")
def players_windowP():
n = 0.030
win = Tk()
win.title('Player Info')
win.geometry("820x520")
win.resizable(False, False)
wrapper1= LabelFrame(win)
mycanvas = Canvas(wrapper1, width=800, height=520)
mycanvas.pack(side=LEFT)
yscrollbar = ttk.Scrollbar(wrapper1, orient="vertical", command= mycanvas.yview)
yscrollbar.pack(side=RIGHT, fill="y")
mycanvas.configure(yscrollcommand=yscrollbar.set)
mycanvas.bind('<Configure>', lambda e: mycanvas.configure(scrollregion = mycanvas.bbox('all')))
myframe = Frame(mycanvas)
mycanvas.create_window((0,0), window=myframe, anchor="nw")
wrapper1.pack(fill="both", expand="yes", padx=0, pady=0)
stats = get_links()['leagueRosterPlayers']
players = get(BASE_URL + stats).json()['league']['standard']
for player in players:
fname = player['firstName']
lname = player['lastName']
jersey = player['jersey']
pos = player ['pos']
height = player['heightFeet']
team = player['lastAffiliation']
data = Label(myframe, text=f"{fname} {lname} - {team}\n Number: {jersey}\n Height: {height} feet\n Position: {pos}",
font=('Arial', 25, 'bold'),
relief = RIDGE,
bd=6,
padx=0,
pady=30,
borderwidth= 5,
justify= LEFT)
data.place(relx= 0, rely= n, anchor=N)
n+= 0.45
data.pack()
players = Button(main_window, text= "Player Info",
font=('Arial', 12, 'bold'),
justify=CENTER,
state=ACTIVE,
bd=1,
command=players_windowP)
players.place(relx=0.5, rely=0.8, anchor=S)
main_window.mainloop()
I made a separate file called clinic1.py for the other code and import it to the main page. Everything works fine however another window appears when I click save button on the add new item page.
When I place all the code on the main page that small window doesn't appear.
I cant find whats causing another window to appear when it's in a separate file.
This is my main page:
from tkinter import *
from tkinter import ttk
import tkinter.messagebox
large_font = ('Verdana',12)
storedusername =['foo'] storedpass=['123'] storedretype=[]
list_of_users=storedusername
list_of_passwords=storedpass
def all_clinic_frames(event):
combo_clinic=combo.get()
if combo_clinic == 'Clinic 1':
enter()
root = Tk()
root.geometry('800x600')
root.title('CSSD')
topFrame=Frame(root,width=800,height=100,padx=310)
area=Label(topFrame,text='CSSD')
area.config(font=("Courier", 50))
frame=Frame(root,highlightbackground="black", highlightcolor="black", highlightthickness=1, width=100, height=100, bd= 0)
frame.place(relx=.5, rely=.5, anchor="center")
username = Label(frame, text='User Name') username.config(font='Arial',width=15) password = Label(frame, text='Password') password.config(font='Arial',width=15) enteruser = Entry(frame, textvariable=StringVar(),font=large_font) enterpass = Entry(frame, show='*', textvariable=StringVar(),font=large_font)
combo_choice=StringVar()
combo=ttk.Combobox(frame,textvariable=combo_choice)
combo['values']=('Clinic 1')
combo.state(['readonly'])
combo.grid(row=0,sticky=NW)
combo.set('Choose Area...')
combo.bind('<<ComboboxSelected>>',all_clinic_frames)
topFrame.grid(row=0,sticky=N) topFrame.grid_propagate(False) area.grid(row=0,column=1,sticky=N) username.grid(row=1, sticky=E) enteruser.grid(row=1, column=1) password.grid(row=2, sticky=E) enterpass.grid(row=2, column=1)
def valid():
usernameRight=enteruser.get()
passwordRight=enterpass.get()
while True:
try:
if (usernameRight==list_of_users[0]) and (passwordRight==list_of_passwords[0]):
import clinic1
clinic1.main_page()
quit()
break
except IndexError:
invalid = Label(frame, text='User name or Password is incorrect!', fg='red')
invalid.grid(row=3, columnspan=2)
break
def enter():
register = Button(frame, text='Sign In',relief=RAISED,fg='white',bg='red',command=valid)
register.grid(row=3,column=1,ipadx=15,sticky=E)
def quit():
root.destroy()
And this is the second file that I imported in the main page which i saved as clinic1.py
from tkinter import*
import tkinter.messagebox
newInstList=[]
def addItem(event=None):
global back_add,quantityentry,itemEntry,itemEntry1,quantityentry1
itemFrameTop=Frame(root, width=800,height=100,bg='pink')
itemFrameTop.grid_propagate(False)
itemFrameTop.grid(row=0)
area1_item = Label(itemFrameTop, text='CSSD', pady=5,padx=230)
area1_item.config(font=("Courier", 30))
area1_item.grid_propagate(False)
area1_item.grid(row=0,column=1,sticky=NE)
clinic_1 = Label(itemFrameTop, text='Clinic 1', bg='red', fg='white', bd=5)
clinic_1.config(font=("Courier", 15))
clinic_1.grid_propagate(False)
clinic_1.grid(row=1, sticky=W,padx=10)
itemFrameMid=Frame(root,width=700,height=600,bg='blue')
itemFrameMid.grid_propagate(False)
itemFrameMid.grid(row=1)
itemname=Label(itemFrameMid,text='Item name:')
itemname.config(font=('Arial,15'))
itemname.grid_propagate(False)
itemname.grid(row=1,sticky=E)
quantity=Label(itemFrameMid,text='Qty:')
quantity.config(font=('Arial,15'))
quantity.grid_propagate(False)
quantity.grid(row=1,column=3, sticky=E,padx=10)
itemEntry=Entry(itemFrameMid)
itemEntry.config(font=('Arial,15'))
itemEntry.grid(row=1,column=1,sticky=EW,padx=30,pady=10)
itemEntry1 = Entry(itemFrameMid)
itemEntry1.config(font=('Arial,15'))
itemEntry1.grid(row=2, column=1)
quantityentry=Entry(itemFrameMid,width=5)
quantityentry.config(font=('Arial',15))
quantityentry.grid(row=1, column=4)
quantityentry1 = Entry(itemFrameMid, width=5)
quantityentry1.config(font=('Arial', 15))
quantityentry1.grid(row=2, column=4,padx=10)
"""When I click save button another small window appears"""
okbutton = Button(itemFrameMid, text='Save', command=saveCheck)
okbutton.config(font=('Arial', 12))
okbutton.grid(row=3, column=4, padx=15)
back_add = Label(itemFrameTop, text='Back')
back_add.config(font=('Courier,15'))
back_add.grid(row=0, sticky=W, padx=30)
back_add.bind('<Button-1>', main_page)
back_add.bind('<Enter>', red_text_back1)
back_add.bind('<Leave>', black_text_back1)
def saveCheck():
saveQuestion=tkinter.messagebox.askquestion('CSSD', 'Are you sure you want to save?')
if saveQuestion == 'yes':
newInstList.append(itemEntry.get())
newInstList.append(quantityentry.get())
newInstList.append(itemEntry1.get())
newInstList.append(quantityentry1.get())
print(newInstList)
main_page()
elif saveQuestion == 'no':
pass
def red_text_back1(event=None):
back_add.config(fg='red')
def black_text_back1(event=None):
back_add.config(fg='black')
def red_text_add(event=None):
addnew.config(fg='red')
def black_text_add(event=None):
addnew.config(fg='black')
def main_page(event=None):
global addnew,usedInst,logOut
frame1 = Frame(root, width=800, height=100,bg='pink')
frame1.grid(row=0, column=0, sticky="nsew")
frame1.grid_propagate(False)
midframe1=Frame(root,width=800,height=600)
midframe1.grid_propagate(False)
midframe1.grid(row=1)
area1 = Label(frame1, text='CSSD',pady=5,padx=350)
area1.config(font=("Courier", 30))
area1.grid(row=0)
clinic1=Label(frame1,text='Clinic 1',bg='red',fg='white',bd=5)
clinic1.config(font=("Courier", 15))
clinic1.grid_propagate(False)
clinic1.grid(row=1,sticky=W,padx=10)
addnew=Label(midframe1,text='+ Add new item')
addnew.config(font=('Arial',15))
addnew.grid(row=2,column=1,sticky=E,ipadx=50)
addnew.bind('<Button-1>', addItem)
addnew.bind('<Enter>', red_text_add)
addnew.bind('<Leave>', black_text_add)
root = Tk()
root.geometry('800x600')
Both files have this line of code:
root = Tk()
Each time you do that, you get another root window. A tkinter application needs to have exactly one instance of Tk running at a time.
You need to remove the last two lines from clinic1.py. You will also need to pass in the reference to root to any methods from clinic1.py that need it.
First file.
from tkinter import *
from tkinter import ttk
import tkinter.messagebox
large_font = ('Verdana',12)
storedusername =['foo']
storedpass=['123']
storedretype=[]
list_of_users=storedusername
list_of_passwords=storedpass
def all_clinic_frames(event):
combo_clinic=combo.get()
if combo_clinic == 'Clinic 1':
enter()
root = Tk()
root.geometry('800x600')
root.title('CSSD')
topFrame=Frame(root,width=800,height=100,padx=310)
area=Label(topFrame,text='CSSD')
area.config(font=("Courier", 50))
frame=Frame(root,highlightbackground="black", highlightcolor="black", highlightthickness=1, width=100, height=100, bd= 0)
frame.place(relx=.5, rely=.5, anchor="center")
myvar=StringVar()
username = Label(frame, text='User Name')
username.config(font='Arial',width=15)
password = Label(frame, text='Password')
password.config(font='Arial',width=15)
enteruser = Entry(frame, textvariable=myvar, font=large_font)
pass1=StringVar()
enterpass = Entry(frame, show='*', textvariable=pass1, font=large_font)
combo_choice=StringVar()
combo=ttk.Combobox(frame,textvariable=combo_choice)
combo['values']=[('Clinic 1')]
combo.state(['readonly'])
combo.grid(row=0,sticky=NW)
combo.set('Choose Area...')
combo.bind('<<ComboboxSelected>>',all_clinic_frames)
topFrame.grid(row=0,sticky=N)
topFrame.grid_propagate(False)
area.grid(row=0,column=1,sticky=N)
username.grid(row=1, sticky=E)
enteruser.grid(row=1, column=1)
password.grid(row=2, sticky=E)
enterpass.grid(row=2, column=1)
def valid():
usernameRight=enteruser.get()
passwordRight=enterpass.get()
while True:
try:
if (usernameRight==list_of_users[0]) and (passwordRight==list_of_passwords[0]):
import clinic1
clinic1.main_page(root)
# quit()
break
except IndexError:
invalid = Label(frame, text='User name or Password is incorrect!', fg='red')
invalid.grid(row=3, columnspan=2)
break
def enter():
register = Button(frame, text='Sign In',relief=RAISED,fg='white',bg='red',command=valid)
register.grid(row=3,column=1,ipadx=15,sticky=E)
def quit():
root.destroy()
root.mainloop()
clinic1.py
from tkinter import*
import tkinter.messagebox
newInstList=[]
def addItem(root, event=None):
global back_add,quantityentry,itemEntry,itemEntry1,quantityentry1
if event is None:
event = Event()
itemFrameTop=Frame(root, width=800, height=100, bg='pink')
itemFrameTop.grid_propagate(False)
itemFrameTop.grid(row=0)
area1_item = Label(itemFrameTop, text='CSSD', pady=5,padx=230)
area1_item.config(font=("Courier", 30))
area1_item.grid_propagate(False)
area1_item.grid(row=0,column=1,sticky=NE)
clinic_1 = Label(itemFrameTop, text='Clinic 1', bg='red', fg='white', bd=5)
clinic_1.config(font=("Courier", 15))
clinic_1.grid_propagate(False)
clinic_1.grid(row=1, sticky=W,padx=10)
itemFrameMid=Frame(root,width=700,height=600,bg='blue')
itemFrameMid.grid_propagate(False)
itemFrameMid.grid(row=1)
itemname=Label(itemFrameMid,text='Item name:')
itemname.config(font=('Arial,15'))
itemname.grid_propagate(False)
itemname.grid(row=1,sticky=E)
quantity=Label(itemFrameMid,text='Qty:')
quantity.config(font=('Arial,15'))
quantity.grid_propagate(False)
quantity.grid(row=1,column=3, sticky=E,padx=10)
itemEntry=Entry(itemFrameMid)
itemEntry.config(font=('Arial,15'))
itemEntry.grid(row=1,column=1,sticky=EW,padx=30,pady=10)
itemEntry1 = Entry(itemFrameMid)
itemEntry1.config(font=('Arial,15'))
itemEntry1.grid(row=2, column=1)
quantityentry=Entry(itemFrameMid,width=5)
quantityentry.config(font=('Arial',15))
quantityentry.grid(row=1, column=4)
quantityentry1 = Entry(itemFrameMid, width=5)
quantityentry1.config(font=('Arial', 15))
quantityentry1.grid(row=2, column=4,padx=10)
"""When I click save button another small window appears"""
okbutton = Button(itemFrameMid, text='Save', command=lambda: saveCheck(root))
okbutton.config(font=('Arial', 12))
okbutton.grid(row=3, column=4, padx=15)
back_add = Label(itemFrameTop, text='Back')
back_add.config(font=('Courier,15'))
back_add.grid(row=0, sticky=W, padx=30)
back_add.bind('<Button-1>', main_page)
back_add.bind('<Enter>', red_text_back1)
back_add.bind('<Leave>', black_text_back1)
def saveCheck(root):
saveQuestion=tkinter.messagebox.askquestion('CSSD', 'Are you sure you want to save?')
if saveQuestion == 'yes':
newInstList.append(itemEntry.get())
newInstList.append(quantityentry.get())
newInstList.append(itemEntry1.get())
newInstList.append(quantityentry1.get())
print(newInstList)
main_page(root)
elif saveQuestion == 'no':
pass
def red_text_back1(event=None):
back_add.config(fg='red')
def black_text_back1(event=None):
back_add.config(fg='black')
def red_text_add(event=None):
addnew.config(fg='red')
def black_text_add(event=None):
addnew.config(fg='black')
def main_page(root):
global addnew,usedInst,logOut
frame1 = Frame(root, width=800, height=100,bg='pink')
frame1.grid(row=0, column=0, sticky="nsew")
frame1.grid_propagate(False)
midframe1=Frame(root,width=800,height=600)
midframe1.grid_propagate(False)
midframe1.grid(row=1)
area1 = Label(frame1, text='CSSD',pady=5,padx=350)
area1.config(font=("Courier", 30))
area1.grid(row=0)
clinic1=Label(frame1,text='Clinic 1',bg='red',fg='white',bd=5)
clinic1.config(font=("Courier", 15))
clinic1.grid_propagate(False)
clinic1.grid(row=1,sticky=W,padx=10)
addnew=Button(midframe1,text='+ Add new item', font=('Arial', 15), command=lambda: addItem(root))
addnew.grid(row=2,column=1,sticky=E,ipadx=50)
# addnew.bind('<Button-1>', lambda r=root: addItem(r))
addnew.bind('<Enter>', red_text_add)
addnew.bind('<Leave>', black_text_add)
I found this introductory tutorial online, but when I run it, I get just a blank window. Any idea what's going wrong?
# A more complex example showing the use of classes in GUI programming
from tkinter import *
class GUIExample:
def __init__(self, master):
self.master = master # for showInfo()
# Create frame to hold widgets
frame = Frame(master)
# Create label for header
self.headLbl = Label(frame, text="Widget Demo Program", relief=RIDGE)
self.headLbl.pack(side=TOP, fill=X)
# Dummy frame for border
spacerFrame = Frame(frame, borderwidth=10)
# Create frame to hold center part of the form
centerFrame = Frame(spacerFrame)
leftColumn = Frame(centerFrame, relief=GROOVE, borderwidth=10)
rightColumn = Frame(centerFrame, relief=GROOVE, borderwidth=10)
# Some colorful widgets
self.colorLabel = Label(rightColumn, text="Select a color")
self.colorLabel.pack(expand=YES, fill=X)
entryText = StringVar(master)
entryText.set("Select a color")
self.colorEntry = Entry(rightColumn, textvariable=entryText)
self.colorEntry.pack(expand=YES, fill=X)
# Create some radio buttons
self.radioBtns = []
self.radioVal = StringVar(master)
btnList = ("black", "red", "green", "blue", "white", "yellow")
for color in btnList:
self.radioBtns.append(Radiobutton(leftColumn, text=color, value=color, \
indicatoron=TRUE, variable=self.radioVal, command=self.updateColor))
else:
if (len(btnList) > 0):
self.radioVal.set(btnList[0])
self.updateColor()
for btn in self.radioBtns:
btn.pack(anchor=W)
# Make the frames visible
leftColumn.pack(side=LEFT, expand=YES, fill=Y)
rightColumn.pack(side=LEFT, expand=YES, fill=BOTH)
centerFrame.pack(side=TOP, expand=YES, fill=BOTH)
# Create an indicator checkbutton
self.indicVal = BooleanVar(master)
self.indicVal.set(TRUE)
self.updateIndic()
Checkbutton(spacerFrame, text="Show indicator", command=self.updateIndic, \
variable=self.indicVal).pack(side=TOP, fill=X)
# Create the Info button
Button(spacerFrame, text="Info", command=self.showInfo).pack(side=TOP, fill=X)
# Create the Quit button
Button(spacerFrame, text="Quit", command=self.quit).pack(side=TOP, fill=X)
def quit(self):
import sys
sys.exit()
def updateColor(self):
self.colorLabel.configure(fg=self.radioVal.get())
self.colorEntry.configure(fg=self.radioVal.get())
def updateIndic(self):
for btn in self.radioBtns:
btn.configure(indicatoron=self.indicVal.get())
def updateColorPrev(self):
if (self.colorprevVal.get()):
for btn in self.radioBtns:
color = btn.cget("text")
btn.configure(fg=color)
else:
for btn in self.radioBtns:
btn.configure(fg="black")
def showInfo(self):
toplevel = Toplevel(self.master, bg="white")
toplevel.transient = self.master
toplevel.title("Program info")
Label(toplevel, text="A simple Tkinter demo", fg="navy", bg="white").pack(pady=20)
Label(toplevel, text="Written by Bruno Dufour", bg = "white").pack()
Label(toplevel, text="http://www.cs.mcgill.ca/ ̃bdufou1/", bg="white").pack()
Button(toplevel, text="Close", command=toplevel.withdraw).pack(pady=30)
root = Tk()
ex = GUIExample(root)
root.title("A simple widget demo")
root.mainloop()
They forgot
frame.pack()
and
spacerFrame.pack()
I didn't test rest of code.
EDIT: BTW: In place of
def quit(self):
import sys
sys.exit()
I would use something more natural - and less drastic
def quit(self):
self.master.destroy()