Tkinter: how do i create multiple of row of label and entry box using loop and inputs from user?
i want to create a simple payroll generator using python tkinter gui. let say the user enter n days in the spinbox, when they press enter it will generate n rows of n day and entry box for user to put working hour.Also how do i get it to display the total hour of n day at the bottom row silmultaneously?
do you know?
how many words do you know
See also questions close to this topic
-
Python File Tagging System does not retrieve nested dictionaries in dictionary
I am building a file tagging system using Python. The idea is simple. Given a directory of files (and files within subdirectories), I want to filter them out using a filter input and tag those files with a word or a phrase.
If I got the following contents in my current directory:
data/ budget.xls world_building_budget.txt a.txt b.exe hello_world.dat world_builder.spec
and I execute the following command in the shell:
py -3 tag_tool.py -filter=world -tag="World-Building Tool"
My output will be:
These files were tagged with "World-Building Tool": data/ world_building_budget.txt hello_world.dat world_builder.spec
My current output isn't exactly like this but basically, I am converting all files and files within subdirectories into a single dictionary like this:
def fs_tree_to_dict(path_): file_token = '' for root, dirs, files in os.walk(path_): tree = {d: fs_tree_to_dict(os.path.join(root, d)) for d in dirs} tree.update({f: file_token for f in files}) return tree
Right now, my dictionary looks like this:
key:''
.In the following function, I am turning the empty values
''
into empty lists (to hold my tags):def empty_str_to_list(d): for k,v in d.items(): if v == '': d[k] = [] elif isinstance(v, dict): empty_str_to_list(v)
When I run my entire code, this is my output:
hello_world.dat ['World-Building Tool'] world_builder.spec ['World-Building Tool']
But it does not see
data/world_building_budget.txt
. This is the full dictionary:{'data': {'world_building_budget.txt': []}, 'a.txt': [], 'hello_world.dat': [], 'b.exe': [], 'world_builder.spec': []}
This is my full code:
import os, argparse def fs_tree_to_dict(path_): file_token = '' for root, dirs, files in os.walk(path_): tree = {d: fs_tree_to_dict(os.path.join(root, d)) for d in dirs} tree.update({f: file_token for f in files}) return tree def empty_str_to_list(d): for k, v in d.items(): if v == '': d[k] = [] elif isinstance(v, dict): empty_str_to_list(v) parser = argparse.ArgumentParser(description="Just an example", formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument("--filter", action="store", help="keyword to filter files") parser.add_argument("--tag", action="store", help="a tag phrase to attach to a file") parser.add_argument("--get_tagged", action="store", help="retrieve files matching an existing tag") args = parser.parse_args() filter = args.filter tag = args.tag get_tagged = args.get_tagged current_dir = os.getcwd() files_dict = fs_tree_to_dict(current_dir) empty_str_to_list(files_dict) for k, v in files_dict.items(): if filter in k: if v == []: v.append(tag) print(k, v) elif isinstance(v, dict): empty_str_to_list(v) if get_tagged in v: print(k, v)
-
Actaully i am working on a project and in it, it is showing no module name pip_internal plz help me for the same. I am using pycharm(conda interpreter
File "C:\Users\pjain\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main return _run_code(code, main_globals, None, File "C:\Users\pjain\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code exec(code, run_globals) File "C:\Users\pjain\AppData\Local\Programs\Python\Python310\Scripts\pip.exe\__main__.py", line 4, in <module> File "C:\Users\pjain\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_internal\__init__.py", line 4, in <module> from pip_internal.utils import _log
I am using pycharm with conda interpreter.
-
Looping the function if the input is not string
I'm new to python (first of all) I have a homework to do a function about checking if an item exists in a dictionary or not.
inventory = {"apple" : 50, "orange" : 50, "pineapple" : 70, "strawberry" : 30} def check_item(): x = input("Enter the fruit's name: ") if not x.isalpha(): print("Error! You need to type the name of the fruit") elif x in inventory: print("Fruit found:", x) print("Inventory available:", inventory[x],"KG") else: print("Fruit not found") check_item()
I want the function to loop again only if the input written is not string. I've tried to type return Under print("Error! You need to type the name of the fruit") but didn't work. Help
-
I ned a while loop statment that makes the calculations loop and ask again
im not too sure how you add a loop statement to this. I want it to be a while loop that makes it do that the calculations repeat after finishing. I've tried but it just keeps on giving me errors. ive tried doing While statements but just will not work im not sure how you set it up as my teacher did not explain very well
import com.godtsoft.diyjava.DIYWindow; public class Calculator extends DIYWindow { public Calculator() { // getting number one double number1 = promptForDouble("Enter a number"); //getting number2 int number2 = promptForInt("Enter an integer"); //getting what to do with operation print("What do you want to do with these numbers?\nAdd\tSubtract\tMultiply\tDivide"); String operation = input(); //declaring variable here so the same one can be used double answer = 0; switch(operation) { case "add": answer = number1 + number2; print(number1 + " + " + number2 + " = " + answer); break; case "subtract": answer = number1 - number2; print(number1 + " - " + number2 + " = " + answer); break; case "multiply": answer = number1 * number2; print(number1 + " * " + number2 + " = " + answer); break; case "divide": try { answer = number1 / number2; } catch(ArithmeticException e) { print("A number cannot be divided by 0."); } print(number1 + " / " + number2 + " = " + answer); break; } double double1 = promptForDouble("Enter a number"); double double2 = promptForDouble("Enter another number"); double double3 = promptForDouble("Enter one last number"); print("What do you want to do with these numbers?\nAdd\tSubtrack\tMultiply\tDivide"); String operation2 = input(); double answer2 = 0; switch(operation2) { case "add": answer2 = double1 + double2 + double3; print(double1 + " + " + double2 + " + " + double3 + " = " + answer2); break; case "subtract": answer2 = double1 - double2 - double3; print(double1 + " - " + double2 + " - " + double3 + " = " + answer2); break; case "multiply": answer2 = double1 * double2 * double3; print(double1 + " * " + double2 + " * " + double3 + " = " + answer2); break; case "divide": try { answer2 = double1 / double2 / double3; } catch(ArithmeticException e) { print("A number cannot be divided by 0."); } print(double1 + " / " + double2 + " / " + double3 + " = " + answer2); break; } want it to loop after the code on top } private double promptForDouble(String prompt) { double number1 = 0; print(prompt); String number = input(); try { number1 = Double.parseDouble(number); } catch(NumberFormatException e){ print("That is not a number. Please enter a number."); number1 = promptForDouble(prompt); } return number1; } private int promptForInt(String prompt) { int number2 = 0; print(prompt); String number = input(); try { number2 = Integer.parseInt(number); } catch(NumberFormatException e) { print("That is not an integer. Enter an integer."); number2 = promptForInt(prompt); } return number2; } public static void main(String[] args) { new Calculator(); } }
-
Data structure for iterating between players
I am new to programming, so I started working on a small guessing game for n players In theory they are supposed to take turns trying to guess some String, I've successfully implemented that game for 1 player, but now I am struggling with how to implement iterating over all players. My code right now looks something like this:
while (!gameIsFinished) { gameIsFinished = player1.takeTurn(); }
I guess I have to use some sort of Data structure to store all my players, but what would I choose in order to be able to iterate over them in a loop?
-
How do I shutil.move my file, post processing, in python based on filename?
My code-
import pandas as pd import datetime as dt import os import shutil path = "C:/Users/rocky/Desktop/autotranscribe/python/Matching" destination_path = ('C:/Users/rocky/Desktop/autotranscribe/python/Matching/Processed') for file in os.listdir("C:/Users/rocky/Desktop/autotranscribe/python/Matching"): if file.startswith("TVC")and "(updated headers)" not in file: dfs = pd.read_excel(file, sheet_name=None) output = dict() for ws, df in dfs.items(): if ws in ["Opt-Ins", "New Voting Members", "Temporary Members"]: continue if ws in ["Voting Members", "Removed Members"]: temp = df dt = pd.to_datetime(os.path.getctime(os.path.join(path,file)),unit="s").replace(nanosecond=0) temp['Status'] = "Active" if ws == "Voting Members" else "Cancelled" output[ws] = temp writer = pd.ExcelWriter(f'{file.replace(".xlsx","")} (updated headers).xlsx') for ws, df in output.items(): df.to_excel(writer, index=None, sheet_name=ws) writer.save() writer.close() shutil.move(path, destination_path )
I just want the file that is being processed here
if file.startswith("TVC")and "(updated headers)" not in file:
to be moved to another folder directory after all my code is processed and the output file is produced..where do i apply shutil.move? i put it outside my inner loop but the file didnt populate in my dest folder as expected. Do i need an if statement? if script runs successfully and output xls produced then move original file to destination folder? -
is there a way to get a new image to display using a function inside of a function?
I'm doing a project for school and I have no clue as to why this isn't working. Any help is appreciated!
from tkinter import * def main(): def change(): photo2 = PhotoImage(file=r"image2") label.configure(image = photo2) mainwindow.destroy() window = Tk() window.geometry("1500x1500") photo1 = PhotoImage(file = r"image1") label = Label(window, image = photo1) label.place(x = 20, y = 20) button = Button(window, text = "Click Me!", command = change) button.place(x = 0, y = 0) window.mainloop() mainwindow = Tk() mainwindow.geometry("700x700") start = Button(mainwindow, text = "start", command = main) start.place(x = 0, y = 0) mainwindow.mainloop()
-
What is the reason of this error I'm getting when using tkinter for a math app
Im making a program that will do most of my homework. Im trying to add some ui and it gives errors in my code. Please tell what's wrong. Make it easy enough for a 13 year old to understand because I'm new to python. This gives an error only when i use canvas. If i use window, then it doesn't but i want to use canvas because I can change their position more accurately
from tkinter import * root=Tk() canvas1 = Canvas(root, width = 400, height = 300) canvas1.pack() entry1 = Entry (root) canvas1.create_window(200, 140, window=entry1) entry2 = Entry (root) canvas1.create_window(200, 180, window=entry2) entry3 = Entry (root) canvas1.create_window(200, 220, window=entry3) def getvalue(): p=entry1.get() r=entry2.get() t=entry3.get() labelans = Label(root, text = float(p*r*t)/100) canvas1.create_window(200, 230, window=labelans) label1 = Label(root, text="Time") canvas1.create_window(437, 220, window=label1) label2 = Label(root, text="Rate") canvas1.create_window(437,180, window=label2) label3 = Label(root, text="Principal") canvas1.create_window(465, 140, window=label3) button1 = Button(text='Solve!', bg="red", command=getvalue) canvas1.create_window(200, 300, window=button1) mainloop()
*And it gives this error
Exception in Tkinter callback Traceback (most recent call last): File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.9/tkinter/__init__.py", line 1892, in __call__ return self.func(*args) File "/data/user/0/ru.iiec.pydroid3/files/temp_iiec_codefile.py", line 17, in getvalue labelans = Label(root, text = float(p*r*t)/100) TypeError: can't multiply sequence by non-int of type 'str' Exception in Tkinter callback Traceback (most recent call last): File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.9/tkinter/__init__.py", line 1892, in __call__ return self.func(*args) File "/data/user/0/ru.iiec.pydroid3/files/temp_iiec_codefile.py", line 17, in getvalue labelans = Label(root, text = float(p*r*t)/100) TypeError: can't multiply sequence by non-int of type 'str' Exception in Tkinter callback Traceback (most recent call last): File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.9/tkinter/__init__.py", line 1892, in __call__ return self.func(*args) File "/data/user/0/ru.iiec.pydroid3/files/temp_iiec_codefile.py", line 17, in getvalue labelans = Label(root, text = float(p*r*t)/100) TypeError: can't multiply sequence by non-int of type 'str' Exception in Tkinter callback Traceback (most recent call last): File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.9/tkinter/__init__.py", line 1892, in __call__ return self.func(*args) File "/data/user/0/ru.iiec.pydroid3/files/temp_iiec_codefile.py", line 17, in getvalue labelans = Label(root, text = float(p*r*t)/100) TypeError: can't multiply sequence by non-int of type 'str' Exception in Tkinter callback Traceback (most recent call last): File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.9/tkinter/__init__.py", line 1892, in __call__ return self.func(*args) File "/data/user/0/ru.iiec.pydroid3/files/temp_iiec_codefile.py", line 17, in getvalue labelans = Label(root, text = float(p*r*t)/100) TypeError: can't multiply sequence by non-int of type 'str'*
-
How to use Tkinter after method to give the impression of a wheel spinning?
I'm trying to create three separate 'wheels' that change the starting position of the first arc in order to give the impression of the wheel spinning. Tkinter will draw the first wheel on the canvas and delete it after 1 second, but the subsequent wheels are never drawn.
from tkinter import Canvas, Tk tk = Tk() tk.geometry('500x500') canvas = Canvas(tk) canvas.pack(expand=True, fill='both') in_list = [1,1,1,1,1,1,1] arc_length = 360 / len(in_list) first_mvmt = arc_length / 3 second_mvmt = arc_length * 2 / 3 mvmt_list = [0, first_mvmt, second_mvmt] for mvmt in mvmt_list: for i in range(len(in_list)): start = i * arc_length extent = (i + 1) * arc_length arc = canvas.create_arc(5, 5, 495, 495, outline='white', start=start + mvmt, extent=extent) canvas.after(1000) canvas.delete('all') tk.mainloop()
Also please excuse my poor formatting, this is my first post on stackoverflow