Can convert Tkinter inputs into numbers - python

__author__ = 'Feuer'
from tkinter import *
root = Tk()
root.geometry("750x400")
def stop():
exit()
def plus():
global erg11
erg11 = z1 + z2
class Start:
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.Label_1 = Label(frame, text="Bitte wählen Sie die Rechenart aus, die Sie benutzen möchten!")
self.Label_2 = Label(frame, text="Addition")
self.Label_3 = Label(frame, text="Subtraktion")
self.Label_4 = Label(frame, text="Multiplikation")
self.Label_5 = Label(frame, text="Division")
self.Label_6 = Label(frame, text="Wurzel")
self.Label_7 = Label(frame, text="Logarithmus")
self.Button_1 = Button(frame, text="Go!", command=Add)
self.Button_2 = Button(frame, text="Go!")
self.Button_3 = Button(frame, text="Go!")
self.Button_4 = Button(frame, text="Go!")
self.Button_5 = Button(frame, text="Go!")
self.Button_6 = Button(frame, text="Go!")
self.Button_7 = Button(frame, text="Das Programm beenden!", command=stop)
self.Label_1.grid(row=0, columnspan=2)
self.Label_2.grid(row=1, column=0)
self.Label_3.grid(row=2, column=0)
self.Label_4.grid(row=3, column=0)
self.Label_5.grid(row=4, column=0)
self.Label_6.grid(row=5, column=0)
self.Label_7.grid(row=6, column=0)
self.Button_1.grid(row=1, column=1)
self.Button_2.grid(row=2, column=1)
self.Button_3.grid(row=3, column=1)
self.Button_4.grid(row=4, column=1)
self.Button_5.grid(row=5, column=1)
self.Button_6.grid(row=6, column=1)
self.Button_7.grid(row=7, columnspan=2)
class Add:
def __init__(self):
newwin = Toplevel()
newwin.geometry("750x400")
frame2 = Frame(newwin)
frame2.pack()
global erg11
global z1
global z2
erg11 = "Ready"
self.Label_1 = Label(frame2, text="Additionsverfahren")
self.Entry_1 = Entry(frame2)
self.Label_2 = Label(frame2, text="+")
self.Entry_2 = Entry(frame2)
self.Label_3 = Label(frame2, text="=")
self.Button_1 = Button(frame2, text="Zurück", command=newwin.destroy)
self.Button_2 = Button(frame2, text="Ergebniss berechnen")
self.Label_Erg1 = Label(frame2, text=erg11)
self.Button_2.bind("<Button-1>", plus)
self.Label_1.grid(row=0, columnspan=4)
self.Entry_1.grid(row=1, column=0)
self.Label_2.grid(row=1, column=1)
self.Entry_2.grid(row=1, column=2)
self.Label_3.grid(row=1, column=3)
self.Button_2.grid(row=2, columnspan=4)
self.Button_1.grid(row=3, columnspan=4)
self.Label_Erg1.grid(row=1, column=4)
app = Start(root)
root.mainloop()
this is my code i am using at the moment.I try to create a little useless calculator with gui in Python. I cant figure out how to get the variable (z1 / z1) out of Entry_1 and _2 when someone is pressing the button_2 (in the second class). Could any one sugesst me some code to fix it?
Edit:
I edited the Code, that everybody could try to find a solution for my problem, because my solution approaches ended in a stalemate. (Ayres)

The content of your entries are read immediately after creation of them, leading get() to return an empty string that can't be converted.
The get method has to be called somewhere else, though I can't exactly say when.

Related

Is there a way to create GUI component in Python Tkinter?

I am writing GUI in Python using Tkinter and I am having some difficulties. I have this repeatable segment:
It repeats 8 times.
I would like to create class or struct of:
frame1 = LabelFrame(root, text="Host 0", padx=5, pady=5)
frame1.grid(row=0, column=0)
labela1 = Label(frame1, text="ID 21")
c1 = Checkbutton(frame1, text="Save images")
c1.grid(row=2, column=1, columnspan=2)
b11 = Button(frame1, text="Start host")
b12 = Button(frame1, text="Start app")
b13 = Button(frame1, text="Kill app")
b14 = Button(frame1, text="Turn off host")
labela1.grid(row=1, column=0)
b11.grid(row=1, column=2)
b12.grid(row=1, column=3)
b13.grid(row=1, column=4)
b14.grid(row=1, column=5)
labela12 = Label(frame1, text="Status", fg='#00f')
labela12.grid(row=2, column=3, columnspan=4)
and then populate GUI through for loop.
Is this possible in python?
I really couldn't find anything on the web for this type of problem.
Thanks in advance!
The most common and easiest way of creating a custom widget is to start by creating a class that inherits from Frame or LabelFrame. Put anything you want inside that class. You can then use that class just like any other widget.
import tkinter as tk
class HostController(tk.LabelFrame):
def __init__(self, parent, hostid, title):
super().__init__(parent, text=title)
self.hostid = hostid
labela1 = tk.Label(self, text=f"ID {hostid}")
c1 = tk.Checkbutton(self, text="Save images")
c1.grid(row=2, column=1, columnspan=2)
b11 = tk.Button(self, text="Start host")
b12 = tk.Button(self, text="Start app")
b13 = tk.Button(self, text="Kill app")
b14 = tk.Button(self, text="Turn off host")
labela1.grid(row=1, column=0)
b11.grid(row=1, column=2)
b12.grid(row=1, column=3)
b13.grid(row=1, column=4)
b14.grid(row=1, column=5)
labela12 = tk.Label(self, text="Status", fg='#00f')
labela12.grid(row=2, column=3, columnspan=4)
root = tk.Tk()
for i in range(5):
hc = HostController(root, hostid=i, title=f"Host {i}")
hc.pack(side="top", padx=4, pady=(0,4))
root.mainloop()

it seems like i can't open a frame in python

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("게임이 종료되었습니다")

How to update Labels in tkinter?

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.

Multiple Windows using Python with Tkinter

I have a school project and I need to create my GUI soon and I cant find an efficient way to do so and I need to make a ceaser Cipher button which brings up a window with an input in there and I need it all to look nice. Here is my code for now. Its not finished and needs to be finished but don't know how to?
from tkinter import *
root = Tk()
root.geometry("180x135")
class Welcome:
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.button1 = Button(topFrame, text ="Decrypter Mode", bg='Black', fg="red", command = self.new_window)
self.button1.pack(side=RIGHT)
self.quitButton = Button(frame, bg='black', fg='white', text='Quit', command=master.destroy)
self.quitButton.pack(side=LEFT)
def new_window(self):
self.newWindow = tk.Toplevel(self.master)
self.app = Demo2(self.newWindow)
topFrame = Frame(root)
topFrame.pack()
bottomFrame = Frame(root)
bottomFrame.pack(side=BOTTOM)
one = Label(root, text='Welcome to Crypto-coder', bg='black', fg='white')
one.pack(fill=X)
one = Label(root, text='', bg='black', fg='white')
one.pack(fill=X,)
one = Label(root, text='', bg='black', fg='white')
one.pack(fill=X,)
one = Label(root, text='', bg='black', fg='white')
one.pack(fill=X,)
button1 = Button(topFrame, text ="Decrypter Mode", bg='Black', fg="red",)
button2 = Button(topFrame, text ="Encoder Mode", bg='black', fg="blue")
button1.pack(side=RIGHT)
button2.pack(side=LEFT)
W=Welcome(root)
root.mainloop()
I had to move some things for the code to show so you may have to fix it if you try it.

in the tkinter module, How would I make the gui wait until input has been registered

I have this as my code, and roughly halfway through is a while loop waiting until the variable self.sub is 1. this is giving me issues, as it will no longer run. it will say its running, but its not. I would be more specific, but I don't know how.
from tkinter import *
root = Tk()
text_width = 100
def destroy_root():
try:
root.destroy()
except:
a = 0
class Application(Frame):
sub = 0
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()
def submit(self):
if len(self.entry.get()) != 0:
self.text.config(width=text_width, state=NORMAL)
self.text.insert(END, '\n'+(str(self.entry.get())))
self.entry.delete(0, END)
self.text.config(width=text_width, state=DISABLED)
self.text.see(END)
self.sub = 1
def story(self):
self.text.config(state=NORMAL)
self.text.insert(END, '\nWelcome to the interactive, text-based game of Dimensia!\n\n_-_-_-_-_-_-_-_-_\n\n'
'Where reality is not what it seems, and parallels are more familiar than ever\n\n')
self.text.config(state=DISABLED)
self.sub = 0
condition = True
while condition:
if self.sub == 1:
condition = False
self.text.config(state=NORMAL)
self.text.insert(END, 'Hello')
self.text.config(state=DISABLED)
self.sub = 0
def createWidgets(self):
self.text = Text(self)
self.text.grid(row=0, column=0, columnspan=2, rowspan=2)
self.text.config(width=text_width, height=45, state=DISABLED)
self.border = Label(self)
self.border.grid(row=0, column=2, rowspan=2)
self.border.config(height=43, background='black')
instruction = 'Welcome to your personal scratchpad!!\n\n'
self.scratch_pad = Text(self)
self.scratch_pad.grid(row=1, column=3, columnspan=2, rowspan=2)
self.scratch_pad.config(width=50, height=30)
self.scratch_pad.insert(END, 'Scratch Pad\n_-_-_-_-_-_-_-_-_-_-_-_-_\n\n\n\n'+str(instruction))
self.QUIT = Button(self, command=self.quit)
self.QUIT.grid(row=2, column=0)
self.QUIT.config(width=1, text='Q')
self.entry = Entry(self)
self.entry.grid(row=2, column=1, columnspan=3)
self.entry.config(width=120)
self.submit_b = Button(self, text='Submit')
self.submit_b.config(command=self.submit)
self.submit_b.grid(row=2, column=4)
frame = Frame(self)
frame.grid(row=0, column=2, columnspan=2)
frame.config(height=1)
health_label = Label(frame, text='Health')
health_label.grid(row=0, column=0)
stamina_label = Label(frame, text='Stamina')
stamina_label.grid(row=2, column=0)
mana_label = Label(frame, text='Mana')
mana_label.grid(row=4, column=0)
courage_label = Label(frame, text='Courage')
courage_label.grid(row=6, column=0)
energy_label = Label(frame, text='Energy')
energy_label.grid(row=0, column=1)
strength_label = Label(frame, text='Strength')
strength_label.grid(row=2, column=1)
knowledge_label = Label(frame, text='Knowledge')
knowledge_label.grid(row=4, column=1)
confusion_label = Label(frame, text='Confusion')
confusion_label.grid(row=6, column=1)
inteligence_label = Label(frame, text='Inteligence')
inteligence_label.grid(row=0, column=2)
agility_label = Label(frame, text='Agility')
agility_label.grid(row=2, column=2)
hunger_label = Label(frame, text='Hunger')
hunger_label.grid(row=4, column=2)
coins_label = Label(frame, text='Coins')
coins_label.grid(row=6, column=2)
curiosity_label = Label(frame, text='Curiosity')
curiosity_label.grid(row=0, column=3)
fear_label = Label(frame, text='Fear')
fear_label.grid(row=2, column=3)
health_value = Spinbox(frame, text='Health', width=5)
health_value.grid(row=1, column=0)
health_value.config(from_=0, to_=100, state=DISABLED)
stamina_value = Spinbox(frame, text='Stamina', width=5)
stamina_value.grid(row=3, column=0)
stamina_value.config(from_=0, to_=100, state=DISABLED)
mana_value = Spinbox(frame, text='Mana', width=5)
mana_value.grid(row=5, column=0)
mana_value.config(from_=0, to_=100, state=DISABLED)
courage_value = Spinbox(frame, text='Courage', width=5)
courage_value.grid(row=7, column=0)
courage_value.config(from_=0, to_=100, state=DISABLED)
strength_value = Spinbox(frame, text='Strength', width=5)
strength_value.grid(row=3, column=1)
strength_value.config(from_=0, to_=100, state=DISABLED)
knowledge_value = Spinbox(frame, text='Knowledge', width=5)
knowledge_value.grid(row=5, column=1)
knowledge_value.config(from_=0, to_=100, state=DISABLED)
confusion_value = Spinbox(frame, text='Confusion', width=5)
confusion_value.grid(row=7, column=1)
confusion_value.config(from_=0, to_=100, state=DISABLED)
energy_value = Spinbox(frame, text='Energy', width=5)
energy_value.grid(row=1, column=1)
energy_value.config(from_=0, to_=100, state=DISABLED)
inteligence_value = Spinbox(frame, text='Inteligence', width=5)
inteligence_value.grid(row=1, column=2)
inteligence_value.config(from_=0, to_=100, state=DISABLED)
agility_value = Spinbox(frame, text='Agility', width=5)
agility_value.grid(row=3, column=2)
agility_value.config(from_=0, to_=100, state=DISABLED)
hunger_value = Spinbox(frame, text='Hunger', width=5)
hunger_value.grid(row=5, column=2)
hunger_value.config(from_=0, to_=100, state=DISABLED)
coins_value = Spinbox(frame, text='Coins', width=5)
coins_value.grid(row=7, column=2)
coins_value.config(from_=0, to_=100, state=DISABLED)
curiosity_value = Spinbox(frame, text='Curiosity', width=5)
curiosity_value.grid(row=1, column=3)
curiosity_value.config(from_=0, to_=100, state=DISABLED)
fear_value = Spinbox(frame, text='Fear', width=5)
fear_value.grid(row=3, column=3)
fear_value.config(from_=0, to_=100, state=DISABLED)
health_value.config(state=NORMAL)
health_value.config(value=100)
health_value.config(state=DISABLED)
stamina_value.config(state=NORMAL)
stamina_value.config(value=100)
stamina_value.config(state=DISABLED)
mana_value.config(state=NORMAL)
mana_value.config(value=0)
mana_value.config(state=DISABLED)
mana_label.config(state=DISABLED)
courage_value.config(state=NORMAL)
courage_value.config(value=50)
courage_value.config(state=DISABLED)
strength_value.config(state=NORMAL)
strength_value.config(value=35)
strength_value.config(state=DISABLED)
knowledge_value.config(state=NORMAL)
knowledge_value.config(value=5)
knowledge_value.config(state=DISABLED)
confusion_value.config(state=NORMAL)
confusion_value.config(value=75)
confusion_value.config(state=DISABLED)
energy_value.config(state=NORMAL)
energy_value.config(value=100)
energy_value.config(state=DISABLED)
inteligence_value.config(state=NORMAL)
inteligence_value.config(value=50)
inteligence_value.config(state=DISABLED)
agility_value.config(state=NORMAL)
agility_value.config(value=50)
agility_value.config(state=DISABLED)
hunger_value.config(state=NORMAL)
hunger_value.config(value=50)
hunger_value.config(state=DISABLED)
coins_value.config(state=NORMAL)
coins_value.config(value=150)
coins_value.config(state=DISABLED)
curiosity_value.config(state=NORMAL)
curiosity_value.config(value=50)
curiosity_value.config(state=DISABLED)
fear_value.config(state=NORMAL)
fear_value.config(value=25)
fear_value.config(state=DISABLED)
self.story()
a = 0
root.wm_title('Dimensia')
app = Application(master=root)
app.mainloop()
destroy_root()
GUIs are already in a perpetual state of waiting for conditions -- this is called the event loop. So, you shouldn't ever need to put in your own loop that waits for something. In fact, your code is waiting, even before your while loop. It waits until the user presses the submit button, then it calls a function.
You need to remove your while loop, and simply respond to the event appropriately in your button callback, or in a function called by your callback.

Categories