I would like to create a class in Tkinter Python 2.7 that creates a new directory using the name introduced by the user in a field after the directory location was chosen from filedialog.
As an example I would like something like this:
User introduces the name of the directory and the following structure should be created:
$HOME\a\<name_introduced_by_the_user_in_the_field>\b
$HOME\a\<name_introduced_by_the_user_in_the_field>\c
I was thinking to start simple and create a simple directory, but I am getting an error.
Here is what I have tried:
class PageThree(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self,text="Insert the name of your project",font=controller.title_font)
label.pack(side="top", fill="x", pady=10)
self.projectnamevar=tk.StringVar()
projectname=tk.Entry(self,textvariable=projectnamevar)
projectname.pack()
button1 = tk.Button(self, text="Create the directory", command=self.create_dir)
button1.pack()
def create_dir(self):
call(["mkdir",projectnamevar.get()])
Error:
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib64/python2.7/lib-tk/Tkinter.py", line 1470, in __call__
return self.func(*args)
File "program.py", line 118, in create_dir
call(["mkdir",self.projectnamevar.get()])
AttributeError: PageThree instance has no attribute 'projectnamevar'
How can I accomplish the whole stuff?
P.S. I am quite new to programming
Your Variable projectnamevar cannot be found in the class as you have not saved it as such, try it with
self.projectnamevar = tk.StringVar()
Also, you might want to use the os module instead of calling it on the system, you can use it like this
import os
path = "~/my/path/"
os.mkdir(path)
import os, sys
if sys.version_info[0] == 3:
from tkinter import *
from tkinter import messagebox
from tkinter import filedialog
from tkinter.ttk import *
elif sys.version_info[0] == 2:
print ("The Script is written for Python 3.6.4 might give issues with python 2.7, let the author know")
print ("Note Python 2.7 CSV has a empty line between each result. couldn't find a fix for that")
from Tkinter import *
import tkMessageBox as messagebox
import tkFileDialog as filedialog
from ttk import Combobox
class temp:
def __init__(self):
self.top = Tk()
self.lab = Label(self.top, text='UserFiled')
self.en = Entry(self.top, width =25)
self.but = Button(self.top, text='Submit',command = self.chooseFolder)
self.lab.grid(row=0, column=0)
self.en.grid(row=0, column=1)
self.but.grid(row=0, column=2)
self.top.mainloop()
def chooseFolder(self):
directory = filedialog.askdirectory()
print(directory)
newPath = os.path.join(directory, self.en.get())
if not os.path.exists(newPath):
os.chdir(directory)
os.mkdir(self.en.get())
obj = temp()
Related
I am trying a "Hello World" test on a GUI window, imported from tkinter. The problem starts when I try to run it and an error pops up in the terminal of VScode:
cannot import name 'geometry' from 'tkinter'.
import csv
from fileinput import filename
import os
from tkinter import *
from tkinter import ttk
from tkinter import Tk, Button, Frame, Entry, END
from tkinter import geometry
class attendance_tester(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
root = Tk()
root.geometry("800x800")
frm = ttk.Frame(root, padding=10)
frm.grid()
ttk.Label(frm, text="Hello World!").grid(column=0, row=0)
ttk.Button(frm, text="Quit", command=root.destroy).grid(column=1, row=0)
app = attendance_tester(master=root)
app.master.title("Student Attendance Program")
app.mainloop()
root.destroy()
from attendance_create import (func_create, func_edit_add,
func_ask_edit, func_edit_change, func_edit_sub,
func_edit_view, func_open, func_first_change)
with open(r"C:\\Users\\user_name\\Desktop\\attendance_sheet.csv") as file:
csv_file_read = csv.reader(file)
for row in csv_file_read:
print(row)
func_open()
attend_test = attendance_tester(root)
root.mainloop()
I have tried seemingly every solution I could find on the internet. I really need some help.
You don't need to import geometry to use it. Just remove the import line and it should work.
you shouldnt import it from the first place you just need to import the tkinter itself just remove that line and it should be just fine
There is no reason to import geometry, it works without that import line.
Also, you have a few duplicate imports which may become confusing. It's better to stick to fewer imports:
from tkinter import *
from tkinter import ttk
from tkinter import Tk, Button, Frame, Entry, END
I'm trying to keep my code clean by separating the GUI from the logic.
Inside of the 'main.py' file, I'd like to call functions from other files that are imported to build the GUI.
The problem is that I cannot figure out how to build a GUI from the 'main.py' file when I try to call another file as an imported module.
Here's what I have in the 'main.py' file:
from tkinter import *
import create_btn
class Main(Tk):
def __init__(self):
super().__init__()
self.title('Main Window')
self.geometry('600x400')
self.eval('tk::PlaceWindow . center')
if __name__ == '__main__':
app = Main()
app.mainloop()
And here is what I have in the 'create_btn.py' file:
from tkinter import *
def createBTN(self):
self.b1 = Button(root, text='B1')
self.b1.pack()
So, how exactly can I build a simple button from another file that I want to import into the 'main.py', or in other words, how do I get the 'create_btn.py' file to build the button inside of the 'main.py' file? A simple example would be greatly appreciated.
I have completely rewritten your code:
# main.py
from tkinter import *
import create_btn
class Main_app:
def __init__(self):
self.root = Tk()
self.root.title("Main Window")
self.geometry("600x400")
self.eval('tk::PlaceWindow . center')
self.button = create_btn.create(self.root)
self.root.mainloop()
a = Main_app()
# create_btn.py
from tkinter import *
class create:
def __init__(self, root):
btn = Button(root, text="Button")
btn.pack()
return btn
This way you can edit the button later in main.py
from PIL import Image
from tkinter import filedialog as fd
import os
import ctypes
import tkinter
class ImageList:
path = ""
dir = ""
top = tkinter.Tk()
canvas = tkinter.Canvas()
canvas.pack()
def __init__(self):
self.path = os.path.expanduser('~/')
def findImage(self):
image = Image.open(self.dir, "rb")
image.show()
def fileExplorer(self, path):
self.canvas.destroy()
self.canvas = tkinter.Canvas()
self.canvas.pack()
obj = os.scandir(path)
for entry in obj:
if entry.is_dir():
#self.path = os.path.abspath(self.path)
b = tkinter.Button(self.canvas, text=entry.name, command=lambda: self.fileExplorer(os.path.abspath(entry).replace('//', '\\')))
b.pack()
elif entry.is_file():
b = tkinter.Button(self.canvas, text=entry.name, command=lambda: print(entry.name))
b.pack()
else:
obj.close()
self.top.mainloop()
As shown in the code above, I am trying to make exe that shows the subdirs and files after C:\Users using buttons in tkinter and repeating it by using recursion.
The first error is that in "os.path.abspath(entry).replace('//', '\')" shows path in a format of "C://Users" and I intended to change that to "C:\Users" using the replace method. However, using "\" doesn't replace "//" and still shows as the original format.
The second error is that after running the code, when I press any button it acts as if I pressed the last button in the tkinter window, meaning that it recursively called the last subdir or the file below "C:\Users".
I'm just getting used to python and this is my first time using stackoverflow. I'm sorry if I did not abide by any of the rules in stackoverflow. Thanks for anyone helping.
Here is the code written so far... The code basically functions as a UI for another Python program. The other python program isn't causing any trouble...
No one has been able to assist me with the previous post so I rephrased and reposted...
import tkinter as tk
from tkinter import ttk
from ttkthemes import themed_tk as tk
import subprocess
import sys
import time
import os
import tkinter.font as font
from tkinter.ttk import *
app = tk.ThemedTk()
app.get_themes()
app.set_theme("radiance")
app.geometry("400x400")
app.configure(bg='gray')
ex_activate_photo = tk.PhotoImage(file=r"C:\Users\bedga\PycharmProjects\GUIdev\ex_button_active.png") #It underlines PhotoImage
myFont = font.Font(family='Helvetica', size=20, weight='normal')
ttk.Label(app, text='Ex', bg='gray', font=(
'Verdana', 15)).pack(side=tk.TOP, pady=10)
app.iconbitmap(r'C:\Users\ex\ex_icon.ico')
def ex_activation():
global pro
print("Ex")
pro = subprocess.Popen("python ex.py", shell=True)
def ex_stop():
global pro
print("Stopping Program... Please Wait!")
os.kill(pro.pid, 0)
ex_activation_button = ttk.Button(app, bg='black', image=ex_activate_photo, width=120, height=120, command=ex_activation)
ex_stop_button = ttk.Button(app, bg='Gray', text='Stop Program', width=12, command=ex_stop, height=3)
ex_stop_button['font'] = myFont
app.title("Ex")
ex_activation_button.pack(side=tk.TOP)
ex_stop_button.pack(side=tk.LEFT)
# app.mainloop()
while True:
try:
app.update()
app.update_idletasks()
except KeyboardInterrupt:
pass
The goal here is to ultimately theme every button (2) and the label at the top. I can then apply similar methods when theming new things in the future. Currently, the PhotoImage is not liking tk and ttk. The program underlines it. One of the buttons being themed is photo-based and the other is text. I have seen successful projects with themed image buttons.
This is the error I get with tk.photoimage
Traceback (most recent call last):
File "C:/Users/ex/main.py", line 19, in <module>
ex_activate_photo = tk.PhotoImage(file=r"C:\Users\ex\ex_button_active.png") #It underlines PhotoImage
AttributeError: module 'ttkthemes.themed_tk' has no attribute 'PhotoImage'
EDIT: This is the error I get for doing
import tkinter as tk
from ttkthemes import themed_tk as tkk
import subprocess
import sys
import time
import os
import tkinter.font as font
from tkinter.ttk import *
I get this error:
Traceback (most recent call last):
File "C:/Users/ex/main.py", line 19, in <module>
ex_activate_photo = tk.PhotoImage(file=r"C:\Users\ex\ex_button_active.png") #It underlines PhotoImage
File "C:\Users\ex\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 3539, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\ex\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 3495, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "C:\Users\ex\PycharmProjects\ex\ex_button_active.png": no such file or directory
I didn't think ttk themes would have an issue with PhotoImage as a variable because it is a theming library for tkinter.
I am very new the GUI development in Python and any help greatly appreciated.
You are importing 2 libraries as tk, that's your main problem. First 3 lines of your code is here
import tkinter as tk
from tkinter import ttk
from ttkthemes import themed_tk as tk
First and third lines have as tk so the latest one is taking over. The error message points to this as well. You should rename one of them.
I have a tkinter based application structured as follows:
import tkinter as tk
class App(tk.Frame):
def __init__(self, master):
self.master = master
tk.Frame.__init__(self, self.master)
self.configure_gui()
self.create_widgets()
def configure_gui(self):
self.master.iconbitmap("my_logo.ico")
self.master.title("Example")
self.master.minsize(250, 50)
def create_widgets(self):
self.label = tk.Label(self.master, text="hello world")
self.label.pack()
if __name__ == "__main__":
root = tk.Tk()
app = App(root)
root.mainloop()
When I run the .py file from the command line, my logo replaces the default tkinter feather logo in the applications main window as expected. I am even able to freeze and bundle my application with pyinstaller using the following command:
pyinstaller -i my_logo.ico my_application.py
Unfortunately, when I attempt to run the .exe file generated by this process I am met with the following error:
Traceback (most recent call last):
File "my_application.py", line 22, in <module>
File "my_application.py", line 7, in __init__
File "my_application.py", line 11, in configure_gui
File "tkinter\__init__.py", line 1865, in wm_iconbitmap
_tkinter.TclError: bitmap "my_logo.ico" not defined
[5200] Failed to execute script my_application
I have scoured this site and others in search of a solution that works in my case and have found none. Any direction would be greatly appreciated!
I've found it easier for me just to store the image as a .py module, then PyInstaller handles it like any other module and the basic command line command to make the exe work without anything special:
Script to make image.py file:
import base64
with open("my_logo.ico", "rb") as image:
b = base64.b64encode(image.read())
with open("image.py", "w") as write_file:
write_file.write("def icon(): return (" + str(b) + ")"
Then import the module as the image:
import tkinter as tk
import image
class App(tk.Frame):
def __init__(self, master):
self.master = master
tk.Frame.__init__(self, self.master)
self.configure_gui()
self.create_widgets()
def configure_gui(self):
self.master.tk.call('wm', 'iconphoto', self.master._w, tk.PhotoImage(data=image.icon()))
self.master.title("Example")
self.master.minsize(250, 50)
def create_widgets(self):
self.label = tk.Label(self.master, text="hello world")
self.label.pack()
if __name__ == "__main__":
root = tk.Tk()
app = App(root)
root.mainloop()
Otherwise you basically have to call out adding the .ico file in the build command, then in your script need to add lines to determine where the unpacked pyinstaller-packed script's directory is, then adjust your path to the packed .ico file.
Discussed here: Bundling data files with PyInstaller (--onefile)
But I find my way easier.