Python tkinter entry problem?
Ich habe ein Programm geschrieben in dem ich aus dem entry tag in tkinter eine zahl haben will. Jedoch ist diese immer 0, kann mir jemand helfen?
2 Antworten
Nimm deinen Programmcode aus der Schleife und, sofern du die test-Funktion öfter aufrufst, auch aus deren Funktionskörper. Es sollte für die Anwendung nur ein einziges Rootfenster mit einer einzigen Event Loop geben.
Für einen Timer kannst du die after-Methode des Rootfensters nutzen.
Beispiel:
def update_time():
now = time.strftime("%H:%M:%S")
some_label.configure(text=now)
your_root_window.after(1000, update_time)
update_time()
Ich will jetzt nicht das ganze Ding abschreiben, um es testen zu können, aber was mir ins Auge fällt ist, dass du alle tkinter-Objekte direkt referenzierst (Tk, Label, Button, Entry etc.), bloß bei den StringVars hast du ein "tk.StringVar()" da stehen. Leider ist bei deinem Code nicht der Teil mit den imports dabei, daher kann ich nicht sehen, wie du tkinter importiert hast. Wenn du aber "from tkinter import *" da stehen hast, musst du bei den StringVars das "tk." wegnehmen, weil das nirgendwo deklariert wird.
Normalerweise müsstest du in der Console auch eine Fehlermeldung kriegen, dass er tk nicht finden kann oder so.
Ich hab mit dem Code mal ein bisschen rumgebastelt. Ich glaube, das Hauptproblem war, dass man den StringVars noch den master mitgeben muss, so wie bei den Widgets. Also mit "StringVar(app)"
import time
from colorama import *
from json import dump, load, dump
import tkinter as tk
from tkinter import *
import random
import os
import random
import json
from datetime import datetime
import io
import base64
from urllib.request import urlopen
def gram_calculator_window():
print("Gram Amount: " + gram_amount_input.get())
print("Purchase Price: " + purchase_price_input.get())
gram_amount = gram_amount_input.get()
purchase_price = purchase_price_input.get()
for x in range(5,21):
calculation = (((int(purchase_price)/int(gram_amount))-x)*-1)
profit = (calculation*int(gram_amount))
if profit < 0:
profit_or_loose = "Loss"
space = ""
else:
profit_or_loose = "Win"
space = " "
if x < 10:
space2 = " "
else:
space2 = " "
ausgabe = "[€]" + space2 + profit_or_loose + ": " + space + str(int(profit))
result_label["text"] = ausgabe
def main():
now = datetime.now()
app = Tk()
app.geometry("600x400")
screen_width = app.winfo_screenwidth()
screen_height = app.winfo_screenheight()
app.geometry()
app.title ("Weed-Calculator | v1.0 | " + now.strftime('%H:%M:%S'))
app.config (background='#094d5e')
global gram_amount_input
global purchase_price_input
gram_amount_input = StringVar(app)
purchase_price_input = StringVar(app)
Label(app, text="Purchase Price", font=("Arial", 12)).grid(row=0, column=0, sticky = "news")
Entry(app, textvariable=purchase_price_input, font=("Arial", 12)).grid(row=0, column=1, sticky = "news")
Label(app, text="Gram Amount", font=("Arial", 12)).grid(row=1, column=0, sticky = "news")
Entry(app, textvariable=gram_amount_input, font=("Arial", 12)).grid(row=1, column=1, sticky = "news")
global result_label
result_label = Label(app, text='', font=("Arial", 12))
result_label.grid(row=2, column=0, columnspan=2, sticky = "news")
Button(app, text="Start", command=gram_calculator_window, font=("Arial", 12)).grid(row=3, column=0, sticky = "news")
Button(app, text="Exit", command=app.destroy, font=("Arial", 12)).grid(row=3, column=1, sticky = "news")
app.mainloop()
if __name__ == '__main__':
main()
import time
from colorama import *
from json import dump, load, dump
import tkinter as tk
from tkinter import *
import random
import os
import random
import json
from datetime import datetime
import io
import base64
from urllib.request import urlopen
def test():
while True:
now = datetime.now()
app = Tk()
app.geometry("1920x1080")
screen_width = app.winfo_screenwidth()
screen_height = app.winfo_screenheight()
app.geometry()
app.title ("Weed-Calculator | v1.0 | " + now.strftime('%H:%M:%S'))
app.config (background='#094d5e')
gram_amount_input= StringVar()
purchase_price_input = StringVar()
Label(app, text="Purchase Price", font=("Arial", 12)).place(x=10, y=10, width=200, height=30)
Entry(app, textvariable=purchase_price_input, text="Gram Calculator", font=("Arial", 12)).place(x=220, y=10 , width=200, height=30)
Label(app, text="Gram Amount", font=("Arial", 12)).place(x=10, y=50, width=200, height=30)
Entry(app, textvariable=gram_amount_input, text="Gram Calculator2", font=("Arial", 12)).place(x=220, y=50 , width=200, height=30)
def gram_calculator_window():
print("Gram Amount: " + gram_amount_input.get())
print("Purchase Price: " + purchase_price_input.get())
gram_amount = gram_amount_input.get()
purchase_price = purchase_price_input.get()
for x in range(5,21):
calculation = (((int(purchase_price)/int(gram_amount))-x)*-1)
profit = (calculation*int(gram_amount))
if profit < 0:
profit_or_loose = "Loss"
space = ""
else:
profit_or_loose = "Win"
space = " "
if x < 10:
space2 = " "
else:
space2 = " "
ausgabe = "[€]" + space2 + profit_or_loose + ": " + space + str(int(profit))
Label(app, text=ausgabe, font=("Arial", 12)).place(x=10, y=30*x, width=300, height=30)
Button(app, text="Start", command=gram_calculator_window, font=("Arial", 12)).place(x=10, y=screen_height-120 , width=100, height=50)
Button(app, text="Exit", command=app.destroy, font=("Arial", 12)).place(x=screen_width-110, y=screen_height-120 , width=100, height=50)
app.mainloop()
Danke für die Antwort hier das ganze Programm. Ich habe beides importiert :)