This is the class I'm calling and the function from a different file
class CalcFunc:
def clearScreen(self):
self.log("CLEAR (CE)")
ent.delete(0, END)
This is the Entry Box
ent = Entry(root, textvariable=clc.getBtn, justify=RIGHT, font=10, relief=RIDGE, bd=2, width=15)
ent.grid(row=0, columnspan=3, pady=10)
This is the button I'm clicking to clear the Entry Box
buttonCC = Button(root, text="CLEAR (CE)", height=1, width=20, bg='orange', command=clc.clearScreen)
I'm not sure what the syntax is to be able to to clear an Entry widget from a class basically. That code worked when I had it in the same file but my project requires it to be in a separate file. It's a class project for a calculator and the "clear" button clears the Entry widget. I can post my entire code if that helps. Thank you.
----EDIT----
My Class
import time
class CalcFunc:
def log(self, val):
myFile = open(r".\log.dat", "a")
myFile.write("%s\n" % val)
myFile.close()
def onScreen(self, iVal):
self.log(iVal)
currentTxt = self.getBtn.get()
updateEnt = self.getBtn.set(currentTxt + iVal)
def clearScreen(self):
self.log("CLEAR (CE)")
ent.delete(0, END)
def evaL(self):
self.log("=")
self.getBtn.set(str(eval(self.getBtn.get())))
self.log(self.getBtn.get())
def logLbl(self):
myFile = open(r".\log.dat", "a")
myFile.write("\n==================================\n")
myFile.write("Date: " + str(time.strftime("%m/%d/%Y")) + " -- Time: " + str(time.strftime("%I:%M:%S")))
myFile.write("\n==================================\n")
myFile.close()
My Program
from tkinter import *
import time
import clcClass
root = Tk()
root.title('skClc v1')
clc = clcClass.CalcFunc()
clc.logLbl()
clc.getBtn = StringVar()
ent = Entry(root, textvariable=clc.getBtn, justify=RIGHT, font=10, relief=RIDGE, bd=2, width=15)
ent.grid(row=0, columnspan=3, pady=10)
button1 = Button(root, text="1", height=1, width=5, bg='light blue', command=lambda:clc.onScreen('1'))
button2 = Button(root, text="2", height=1, width=5, bg='light blue', command=lambda:clc.onScreen('2'))
button3 = Button(root, text="3", height=1, width=5, bg='light blue', command=lambda:clc.onScreen('3'))
button4 = Button(root, text="4", height=1, width=5, bg='light blue', command=lambda:clc.onScreen('4'))
button5 = Button(root, text="5", height=1, width=5, bg='light blue', command=lambda:clc.onScreen('5'))
button6 = Button(root, text="6", height=1, width=5, bg='light blue', command=lambda:clc.onScreen('6'))
button7 = Button(root, text="7", height=1, width=5, bg='light blue', command=lambda:clc.onScreen('7'))
button8 = Button(root, text="8", height=1, width=5, bg='light blue', command=lambda:clc.onScreen('8'))
button9 = Button(root, text="9", height=1, width=5, bg='light blue', command=lambda:clc.onScreen('9'))
button0 = Button(root, text="0", height=1, width=5, bg='light blue', command=lambda:onScreen('0'))
buttonP = Button(root, text="+", height=1, width=5, bg='gray', command=lambda:clc.onScreen('+'))
buttonM = Button(root, text="-", height=1, width=5, bg='gray', command=lambda:clc.onScreen('-'))
buttonMM = Button(root, text="x", height=1, width=5, bg='gray', command=lambda:clc.onScreen('*'))
buttonDD = Button(root, text="÷", height=1, width=5, bg='gray', command=lambda:clc.onScreen('/'))
buttonEE = Button(root, text="=", height=1, width=5, bg='light green', command=clc.evaL)
buttonCC = Button(root, text="CLEAR (CE)", height=1, width=20, bg='orange', command=clc.clearScreen)
button1.grid(row=1, column=0, pady=5)
button2.grid(row=1, column=1, pady=5)
button3.grid(row=1, column=2, pady=5)
button4.grid(row=2, column=0, pady=5)
button5.grid(row=2, column=1, pady=5)
button6.grid(row=2, column=2, pady=5)
button7.grid(row=3, column=0, pady=5)
button8.grid(row=3, column=1, pady=5)
button9.grid(row=3, column=2, pady=5)
button0.grid(row=4, column=0, pady=5)
buttonP.grid(row=4, column=1, pady=5)
buttonM.grid(row=4, column=2, pady=5)
buttonEE.grid(row=5, column=0, pady=5)
buttonDD.grid(row=5, column=1, pady=5)
buttonMM.grid(row=5, column=2, pady=5)
buttonCC.grid(row=6, column=0, pady=5, columnspan=3)
root.maxsize(140,245);
root.minsize(140,245);
root.mainloop()
ent = Entry(root, ....)
clc = clcClass.CalcFunc(ent)
class CalcFunc:
def __init__(self, entry):
self.entry = entry
def clearScreen(self):
self.log("CLEAR (CE)")
self.entry.delete(0, END)
Here's an abbreviated example:
#my_entry.py
from tkinter import END
import time
class EntryWithLogger:
def __init__(self, entry):
self.entry = entry
def log(self, val):
with open("log.dat", "a") as my_file: #Automatically closes the file--even if an exception occurs, which is not the case with my_file.close().
my_file.write("%s\n" % val)
def onScreen(self, i_val):
self.log(i_val)
self.entry.insert(END, i_val)
def clearScreen(self):
self.log("CLEAR (CE)")
self.entry.delete(0, END)
Note that I didn't use a StringVar(), which doesn't appear to be necessary. If you need it, you can always pass it as an argument to __init__(), then store it on self.
import my_entry as me
import tkinter as tk
root = tk.Tk()
root.title("Calculator")
root.geometry("+100+50") #("300x500+200+10") dimension, position
entry = tk.Entry(root, justify=tk.RIGHT, font=10, relief=tk.RIDGE, bd=2, width=15)
entry.grid(row=0, columnspan=3, pady=10)
entry_with_logger = me.EntryWithLogger(entry)
#Create the buttons in a loop:
for i in range(10):
row_num, col_num = divmod(i, 3) #divmod(7, 2) => (3, 1), divmod(0, 3) => (0, 0), divmod(4, 3) => (1, 1)
row_num += 1
button_text = str(i)
tk.Button(root, text=button_text,
height=1,
width=5,
bg='light blue',
command=lambda x=button_text: entry_with_logger.onScreen(x)
).grid(row=row_num, column=col_num, pady=5)
#Put the clear button at the bottom of the grid:
tk.Button(root, text="CLEAR (CE)",
height=1,
width=20,
bg='orange',
command=entry_with_logger.clearScreen
).grid(row=row_num+1, columnspan=3) #columnspan tells grid() to use 3 cells for the button,
#and the button will be centered by default.
root.mainloop()
Or, you could do it like this:
#my_entry.py
from tkinter import Entry, END
import time
class EntryWithLogger(Entry):
#Because __init__() is not implemented, the parent class's __init__() gets
#called, so you create an EntryWithLogger just like you would an Entry.
def log(self, val):
with open("log.dat", "a") as my_file: #Automatically closes the file--even if there is an exception, which is not the case with my_file.close().
my_file.write("%s\n" % val)
def onScreen(self, i_val):
self.log(i_val)
self.insert(END, i_val)
def clearScreen(self):
self.log("CLEAR (CE)")
self.delete(0, END)
import my_entry as me
import tkinter as tk
root = tk.Tk()
root.title("Calculator")
root.geometry("+100+50") #("300x500+200+10") dimension, position
entry = me.EntryWithLogger(root, justify=tk.RIGHT, font=10, relief=tk.RIDGE, bd=2, width=15)
entry.grid(row=0, columnspan=3, pady=10)
#Create the buttons in a loop:
for i in range(10):
row_num, col_num = divmod(i, 3) #divmod(7, 2) => (3, 1), divmod(0, 3) => (0, 0), divmod(4, 3) => (1, 1)
row_num += 1
button_text = str(i)
tk.Button(root, text=button_text,
height=1,
width=5,
bg='LightBlue',
command=lambda x=button_text: entry.onScreen(x)
).grid(row=row_num, column=col_num, pady=5)
#Put the clear button at the bottom of the grid:
tk.Button(root, text="CLEAR (CE)",
height=1,
width=20,
bg='orange',
command=entry.clearScreen
).grid(row=row_num+1, columnspan=3) #columnspan tells grid() to use 3 cells for the button,
#and the button will be centered by default.
root.mainloop()
Related
i want to open the menu_frame by clicking the login_button but the frame won't come up and there are no error messages showing up. its my first time and im so lost
ive tried to google how to fix this problem but from what ive read, it seems to me that there are no errors or any reason for this code to not function properly. please help :(
from tkinter import *
window = Tk()
window.title("EL TALLO")
window.geometry("700x490")
window.config(background="#FFF8E5")
#회원가입
def register_frame():
register_frame = Frame(
window,
bd=2,
bg='#FFF8E5',
relief=SOLID,
padx=10,
pady=10
)
Label(
register_frame,
text="ID입력",
bg='#CCCCCC',
).grid(row=0, column=0, sticky=W, pady=10)
Label(
register_frame,
text="비밀번호 입력",
bg='#CCCCCC',
).grid(row=5, column=0, sticky=W, pady=10)
newlyset_id = Entry(
register_frame
)
newlyset_pw = Entry(
register_frame,
show='*'
)
register_btn = Button(
register_frame,
width=15,
text='회원가입',
relief=SOLID,
cursor='hand2',
command=register_frame.destroy
)
newlyset_id.grid(row=0, column=1, pady=10, padx=20)
newlyset_pw.grid(row=5, column=1, pady=10, padx=20)
register_btn.grid(row=7, column=1, pady=10, padx=20)
register_frame.pack()
register_frame.place(x=220, y=150)
def new_id(): #new_id에 newlyset_id에 입력한 값을 저장
new_id = newlyset_id.get()
def new_pw(): #new_pw에 newlyset_pw에 입력한 값을 저장
new_pw = newlyset_pw.get()
#메뉴화면
def menu_frame():
menu_frame = Frame(
window,
bd=2,
bg='#FFF8E5',
relief=SOLID,
padx=10,
pady=10
)
label1 = Label(menu_frame, text = "EL TALLO", bg="lightgreen",width=10, height=1, font=(15))
label1.pack()
btn1 = Button(menu_frame, text = "play game", bg="gray", width=15, height=1)
btn1.pack()
btn2 = Button(menu_frame, text = "How to play", bg="gray", width=15, height=1)
btn2.pack()
btn3 = Button(menu_frame, text = "Settings", bg="gray", width=15, height=1)
btn3.pack()
def btncmd():
print("게임이 종료되었습니다")
btn4 = Button(menu_frame, text = "END GAME", command=btncmd, bg="lightgreen", width=15, height=1)
btn4.pack()
label1.place(x=50, y=50)
btn1.place(x=50, y=100)
btn2.place(x=50, y=150)
btn3.place(x=50, y=200)
btn4.place(x=50, y=250)
#로그인
Label(
window,
text="아이디 입력",
bg='#CCCCCC',
).place(x=230, y=170)
id_tf = Entry(
window,
).place(x=330, y=170)
def id(): #id에 id_tf에 입력한 값을 저장
id = id_tf.get()
Label(
window,
text="비밀번호 입력",
bg='#CCCCCC',
).place(x=230, y=220)
pw_tf = Entry(
window,
).place(x=330, y=220)
def pw(): #pw에 pw_tf에 입력한 값을 저장
pw = pw_tf.get()
#회원가입 버튼
registerbutton = Button(
window,
width=15,
text="회원가입",
bg="#CCCCCC",
cursor='hand2',
command=register_frame
)
registerbutton.place(x=360, y=270)
#로그인 버튼
loginbutton = Button(
window,
width=15,
text="로그인",
bg="#CCCCCC",
cursor='hand2',
command=menu_frame
)
loginbutton.place(x=230, y=270)
window.mainloop()
You didn't pack the menu_frame and the indentation of def btncmd() was wrong.
That is:
btn3 = Button(menu_frame, text = "Settings", bg="gray", width=15, height=1)
btn3.pack()
menu_frame.pack()
def btncmd():
print("게임이 종료되었습니다")
I'm a beginner, and I've gathered a number of reference sites to make code!
But if you click the button, you'll see the value only for the last item.
Do you happen to know how to get each result in every row! And now you have to enter all the items to get the value, but even if there is a blank, I hope the result will come out for all the input boxes.
I think the code is a little tangled, but it's hard to solve. I'm posting the code's full text.
from tkinter import *
from tkinter import ttk
import csv
class App(ttk.Frame):
num_rows = 2
entry_list = []
def __init__(self, master, *args, **kwargs):
ttk.Frame.__init__(self, master, *args, **kwargs)
self.master = master
self.label_frame = ttk.Frame(self.master)
self.label_frame.grid()
self.label_list = []
label1 = Label(root, width=5, text="No.")
label2 = Label(root, width=12, text="Chip")
label3 = Label(root, width=12, text="Length[mm] : ")
label4 = Label(root, width=12, text="Width[mm] : ")
label5 = Label(root, width=12, text="Height[mm] : ")
label6 = Label(root, width=12, text="Watt[W] : ")
label7 = Label(root, width=12, text="Rjc[C/W] : ")
label8 = Label(root, width=12, text="h[W/㎡K] : ")
label9 = Label(master, width=12, text="Temp[C] : ",fg='red')
#grid
label1.grid(row=0, column=0, padx=1, pady=10)
label2.grid(row=0, column=1, padx=2, pady=10)
label3.grid(row=0, column=2, padx=2, pady=10)
label4.grid(row=0, column=3, padx=2, pady=10)
label5.grid(row=0, column=4, padx=2, pady=10)
label6.grid(row=0, column=5, padx=2, pady=10)
label7.grid(row=0, column=6, padx=2, pady=10)
label8.grid(row=0, column=7, padx=2, pady=10)
label9.grid(row=0, column=8, padx=2, pady=10)
##유동
self.Number_field = Entry(root, width=5)
self.Length_field = Entry(root, width=9)
self.Width_field = Entry(root, width=9)
self.Height_field = Entry(root, width=9)
self.Watt_field = Entry(root, width=9)
self.Rjc_field = Entry(root, width=9)
self.Temperature_field = Entry(root, width=9)
self.Number_field.grid(row=1, column=0, padx=1, pady=1)
self.Length_field.grid(row=1, column=2, padx=1, pady=1)
self.Width_field.grid(row=1, column=3, padx=1, pady=1)
self.Height_field.grid(row=1, column=4, padx=1, pady=1)
self.Watt_field.grid(row=1, column=5, padx=1, pady=1)
self.Rjc_field.grid(row=1, column=6, padx=1, pady=1)
self.Temperature_field.grid(row=1, column=8, padx=1, pady=1)
strs = StringVar()
strs2 = StringVar()
combx1 = ttk.Combobox(root, width=9, textvariable=strs)
combx1['values'] = (' Natural', ' Forced(Fan)')
combx1.current()
combx1.grid(column=7, row=1)
combx2 = ttk.Combobox(root, width=9, textvariable=strs2)
combx2['values'] = ('CPU', 'eMMC', 'PMIC')
combx2.grid(column=1, row=1)
combx2.current()
# Create a Submit Button and attached
button1 = Button(root, text="Submit", bg="black", fg="white", command=self.cal_Temp)
button1.grid(row=100, column=0, pady=10)
button2 = Button(root, text="Clear", fg="blue", command=self.clear_all)
button2.grid(row=100, column=1, pady=10)
button3 = Button(root, text="Add Row (+)", command=self.add_new)
button3.grid(row=102, column=0, pady=10)
button4 = Button(root, text="Delete Row (-)", command=self.delete)
button4.grid(row=102, column=1, pady=10)
button5 = Button(root, text="Export", command=self.writeToFile)
button5.grid(row=103, column=1, pady=10)
def writeToFile(self):
with open('Temperature_MJH.csv', 'a') as f:
w=csv.writer(f, quoting=csv.QUOTE_ALL)
w.writerow([self.Length_field.get()])
w.writerow([self.Width_field.get()])
w.writerow([self.Height_field.get()])
w.writerow([self.Watt_field.get()])
w.writerow([self.Rjc_field.get()])
w.writerow([self.Temperature_field.get()])
def clear_all(self):
self.Number_field.delete(0,END)
self.Length_field.delete(0, END)
self.Width_field.delete(0, END)
self.Height_field.delete(0, END)
self.Watt_field.delete(0, END)
self.Rjc_field.delete(0, END)
self.Temperature_field.delete(0, END)
#이거 지워도 되는지
self.Length_field.focus_set()
def cal_Temp(self):
Length = float(self.Length_field.get())
Width = float(self.Width_field.get())
Height = float(self.Height_field.get())
Watt = float(self.Watt_field.get())
Rjc = float(self.Rjc_field.get())
h = 1
Temperature = float(((Watt * 0.5) / (((Length * Width * 2) + (Length * 2) + (
Height * Width * 5)) / 1000)) / h + Rjc )
self.Temperature_field.insert(10,Temperature)
print(Temperature)
def add_new(self):
self.num_rows += 1
self.Number_field = Entry(root, width=5)
self.Length_field = Entry(root, width=9)
self.Width_field = Entry(root, width=9)
self.Height_field = Entry(root, width=9)
self.Watt_field = Entry(root, width=9)
self.Rjc_field = Entry(root, width=9)
self.Temperature_field = Entry(root, width=9)
self.Number_field.grid(row=self.num_rows, column=0, padx=1, pady=1)
self.Length_field.grid(row=self.num_rows, column=2, padx=1, pady=1)
self.Width_field.grid(row=self.num_rows, column=3, padx=1, pady=1)
self.Height_field.grid(row=self.num_rows, column=4, padx=1, pady=1)
self.Watt_field.grid(row=self.num_rows, column=5, padx=1, pady=1)
self.Rjc_field.grid(row=self.num_rows, column=6, padx=1, pady=1)
self.Temperature_field.grid(row=self.num_rows, column=8, padx=1, pady=1)
strs = StringVar()
strs2 = StringVar()
self.combx1 = ttk.Combobox(root, width=9, textvariable=strs)
self.combx1['values'] = (' Natural', ' Forced(Fan)')
self.combx1.current()
self.combx1.grid(row=self.num_rows, column=7)
self.combx2 = ttk.Combobox(root, width=9, textvariable=strs2)
self.combx2['values'] = ('CPU', 'eMMC', 'PMIC')
self.combx2.grid(row=self.num_rows, column=1)
self.combx2.current()
self.ics=self.num_rows
self.Number_field.insert(10, self.ics-1)
def delete(self):
self.num_rows -= 1
self.Number_field.destroy()
self.Length_field.destroy()
self.Width_field.destroy()
self.Height_field.destroy()
self.Watt_field.destroy()
self.Rjc_field.destroy()
self.Temperature_field.destroy()
self.combx1.destroy()
self.combx2.destroy()
self.ics -= 1
if __name__ == "__main__":
root = Tk()
root.configure(background='snow')
root.geometry("1000x450")
root.title("Expectation of Temp Calculator")
my_app = App(root)
root.mainloop()
Hello I am preparing a GUI for a project, but unfortunately I cant enter a specific widget with my numpad at once. If I click a number on my touchscreen numpad, the digit appears in both widgets. How can I change it, that I can only enter a digit in a widget at once? I wanted to try it with the focus method but it didnt really work out...
Thanks for your help.
Here is my code:
# globally declare the expression variable
expression = ""
# Function to update expression
# in the text entry box
def press(num):
# point out the global expression variable
global expression
# concatenation of string
expression = expression + str(num)
# update the expression by using set method
equation1.set(expression)
equation2.set(expression)
# Function to clear the contents
# of text entry box
def clear():
global expression
expression = ""
equation1.set("")
equation2.set("")
#create GUI
# Driver code
if __name__ == "__main__":
# create a GUI window
gui = Tk()
gui.title("GUI")
equation1 = StringVar()
equation2 = StringVar()
equation1.set("")
equation2.set("")
#Label 1
label1 = tkinter.Label(text ="Pumpenhöhe 1 [cm]")
label1.grid (row =0 , column =0 , padx = xdis , pady = ydis )
#Eingabefeld 1 definieren
eingabe1 = tkinter.Entry(gui, textvariable=equation1, width=4, bg ='#ffffff')
eingabe1.grid(row=0, column=1, padx=xdis, pady = ydis)
#Label 2
label2 = tkinter.Label (text ="Pumpenhöhe 2 [cm]")
label2.grid(row=1,column =0 , padx = xdis ,pady = ydis)
#Eingabefeld 2
eingabe2 = tkinter.Entry(gui, textvariable=equation2, width=4, bg ='#ffffff')
eingabe2.grid(row=1, column=1, padx=xdis, pady = ydis)
#button obj on framework to send values
set_setpoints = tkinter.Button(text ="Send", command = set_setpoints)
set_setpoints.grid(row=2, column=2, padx= xdis, pady = ydis)
#create exit button
ex_bt = tkinter.Button(gui, text='Exit', command=gui.destroy)
ex_bt.grid(row=7, column=2, sticky=tkinter.W, padx=xdis, pady=ydis)
#buttons for numpad
button1 = Button(gui, text=' 1 ',
command=lambda: press(1), height=1, width=7)
button1.grid(row=3, column=0)
button2 = Button(gui, text=' 2 ',
command=lambda: press(2), height=1, width=7)
button2.grid(row=3, column=1)
button3 = Button(gui, text=' 3 ',
command=lambda: press(3), height=1, width=7)
button3.grid(row=3, column=2)
button4 = Button(gui, text=' 4 ',
command=lambda: press(4), height=1, width=7)
button4.grid(row=4, column=0)
button5 = Button(gui, text=' 5 ',
command=lambda: press(5), height=1, width=7)
button5.grid(row=4, column=1)
button6 = Button(gui, text=' 6 ',
command=lambda: press(6), height=1, width=7)
button6.grid(row=4, column=2)
button7 = Button(gui, text=' 7 ',
command=lambda: press(7), height=1, width=7)
button7.grid(row=5, column=0)
button8 = Button(gui, text=' 8 ',
command=lambda: press(8), height=1, width=7)
button8.grid(row=5, column=1)
button9 = Button(gui, text=' 9 ',
command=lambda: press(9), height=1, width=7)
button9.grid(row=5, column=2)
button0 = Button(gui, text=' 0 ',
command=lambda: press(0), height=1, width=7)
button0.grid(row=6, column=0)
clear = Button(gui, text='Clear',
command=clear, height=1, width=7)
clear.grid(row=6, column='1')
Decimal= Button(gui, text='.',
command=lambda: press('.'), height=1, width=7)
Decimal.grid(row=6, column=2)
gui.mainloop()
I could solve it like this:
#add text inside widget
def set_text(text):
widget = gui.focus_get()
if widget in [entry1, entry2]:
widget.insert("insert", text)
def backspace():#Delete one digit at a time into perticular entry field
pass
def clear():#Clear text in perticular entry field
entry1.delete(0,END)
entry2.delete(0,END)
#create GUI
# Driver code
if __name__ == "__main__":
# create a GUI window
gui = Tk()
gui.title("GUI")
#Label 1
label1 = tkinter.Label(text ="Pumpenhöhe 1 [cm]")
label1.grid (row =0 , column =0 , padx = xdis , pady = ydis )
#Eingabefeld 1 definieren
entry1 = tkinter.Entry(gui, textvariable=StringVar(), width=4, bg ='#ffffff')
entry1.grid(row=0, column=1, padx=xdis, pady = ydis)
#Label 2
label2 = tkinter.Label (text ="Pumpenhöhe 2 [cm]")
label2.grid(row=1,column =0 , padx = xdis ,pady = ydis)
#Eingabefeld 2
entry2 = tkinter.Entry(gui, textvariable=StringVar(), width=4, bg ='#ffffff')
entry2.grid(row=1, column=1, padx=xdis, pady = ydis)
#create exit button
ex_bt = tkinter.Button(gui, text='Exit', command=gui.destroy)
ex_bt.grid(row=4, column=2, sticky=tkinter.W, padx=xdis, pady=ydis)
#button obj to start thread
#start_thread = tkinter.Button(text ="start thread(main loop)", command=start_thread)
#start_thread.grid(row=2, column=1, padx=xdis, pady = ydis)
#button obj to stop thread
#stop_thread = tkinter.Button(text ="Stop", command=stop_thread)
#stop_thread.grid(row=2, column=3, padx=xdis, pady = ydis)
#button obj on framework to send values
set_setpoints = tkinter.Button(text ="Send", command = set_setpoints)
set_setpoints.grid(row=2, column=2, padx= xdis, pady = ydis)
#buttons for numpad
tkinter.Button(gui, text="7", command=lambda: set_text("7"),height=1, width=7).grid(row=5, column=0)
tkinter.Button(gui, text="8", command=lambda: set_text("8"),height=1, width=7).grid(row=5, column=1)
tkinter.Button(gui, text="9", command=lambda: set_text("9"),height=1, width=7).grid(row=5, column=2)
tkinter.Button(gui, text="4", command=lambda: set_text("4"),height=1, width=7).grid(row=6, column=0)
tkinter.Button(gui, text="5", command=lambda: set_text("5"),height=1, width=7).grid(row=6, column=1)
tkinter.Button(gui, text="6", command=lambda: set_text("6"),height=1, width=7).grid(row=6, column=2)
tkinter.Button(gui, text="1", command=lambda: set_text("1"),height=1, width=7).grid(row=7, column=0)
tkinter.Button(gui, text="2", command=lambda: set_text("2"),height=1, width=7).grid(row=7, column=1)
tkinter.Button(gui, text="3", command=lambda: set_text("3"),height=1, width=7).grid(row=7, column=2)
tkinter.Button(gui, text="0", command=lambda: set_text("0"),height=1, width=7).grid(row=7, column=1)
backspace = tkinter.Button(gui, text='<-', command = lambda:backspace())
backspace.grid(row=3,column=1, padx=xdis, pady=ydis)
clear_btn = Button(gui, text='C', command = lambda:clear())
clear_btn.grid(row=3,column=2, padx=xdis, pady=ydis)
gui.mainloop()
I'm trying to adjust the buttons, but somehow even after changing the width, it won't change the size. This is my code:
if __name__ == '__main__':
# create application window
app = Tk()
# title
app.title("Music Players")
# geometry
app.geometry('500x300')
# background color
app.configure(bg='orange')
equation = StringVar()
window_1 = Label(app, textvariable=equation)
window_1.grid(columnspan=3, ipadx=100, ipady=10)
equation.set('music player')
window_2 = Entry(app, width=30)
window_2.grid(columnspan=5, ipadx=100, ipady=10)
# Create buttons and other accessories
button1 = Button(app, text='PLAY', fg='yellow', bg='purple',
command=lambda: press('PLAY'), height=2, width=1)
button1.grid(row=2, column=0, sticky="NSEW")
button2 = Button(app, text='STOP', fg='yellow', bg='purple',
command=lambda: press('STOP'), height=2, width=2)
button2.grid(row=2, column=1, sticky="NSEW")
button3 = Button(app, text='NEXT', fg='yellow', bg='purple',
command=lambda: press('NEXT'), height=2, width=2)
button3.grid(row=2, column=2, sticky="NSEW")
button4 = Button(app, text='PREVIOUS', fg='yellow', bg='purple',
command=lambda: press('PREVIOUS'), height=2, width=2)
button4.grid(row=2, column=3, sticky="NSEW")
I'm assuming it is because of sticky="NSEW", because when I remove it, the buttons change the size, but there is big spaces between the buttons and also, very spaced out, even though columns=0 and columns=1.
How can I change the size of buttons without getting rid of sticky? Or, without sticky, but having the buttons next to each other?
My expected output will be like this:
Thank you in advance!
I'm trying to create a program in where you put a word in a box, press add, and this word goes to a list, which is also displayed on the right side. When I press the forward button the first thing on the list is deleted. Problem is I can't get the labels to update when I press the buttons / edit the list.
from tkinter import *
root = Tk()
root.title('Speakers List')
root.minsize(800, 600)
speakers = ['none']
spe = speakers[0]
def add():
if spe == 'none':
speakers.insert(0, [s])
e.delete(0, END)
spe.config(text=speakers[0])
else:
speakers[-2] = [s]
e.delete(0, END)
spe.config(text=speakers[0])
return
def forward():
if len(speakers) is 0:
return
else:
del speakers[0]
spe.config(text=speakers[0])
return
entry = StringVar()
e = Entry(root, width=30, font=("Arial", 20), textvariable=entry)
e.grid(row=0, sticky=W)
s = e.get()
button1 = Button(root, padx=10, pady=10, bd=5, text='Add', fg='black', command=add)
button1.grid(row=0, column=1)
button2 = Button(root, padx=10, pady=10, bd=5, text='Next', fg='black', command=forward)
button2.grid(row=1, column=1)
n = Label(root, font=("Arial", 35), bd=2, text=spe)
n.grid(row=1, sticky=W)
listdisplay = Label(root, font=('Arial', 20), text=speakers)
listdisplay.grid(row=0, column=10)
root.mainloop()
Is this the sort of thing you were looking for ?
from tkinter import *
root = Tk()
root.title('Speakers List')
root.minsize(800, 600)
speakers = ['50']
spe = speakers[0]
def add():
entry=e.get()
speakers.append(entry)
listdisplay.config(text=speakers)
return
def forward():
if len(speakers) is 0:
return
else:
del speakers[0]
listdisplay.config(text=speakers)
spe=speakers[0]
n.config(text=spe)
return
entry = StringVar()
e = Entry(root, width=30, font=("Arial", 20), textvariable=entry)
e.grid(row=0, sticky=W)
s = e.get()
button1 = Button(root, padx=10, pady=10, bd=5, text='Add', fg='black',command=add)
button1.grid(row=0, column=1)
button2 = Button(root, padx=10, pady=10, bd=5, text='Next', fg='black',command=forward)
button2.grid(row=1, column=1)
n = Label(root, font=("Arial", 35), bd=2, text=spe)
n.grid(row=1, sticky=W)
listdisplay = Label(root, font=('Arial', 20), text=speakers)
listdisplay.grid(row=0, column=10)
root.mainloop()
If so:
You create a list and then you use the append function to add an item to it. The rest was pretty much right.