Burada bir hata var. gui.py
'nin içinde gui
'yi import
ediyorsunuz.
Size basit bir örnek göstereyim.
Projenin ağaç görünümünün aşağıda gösterildiği gibi olduğunu varsayalım.
.
├── run.py
└── Scripts
├── button.py
├── entry.py
├── __init__.py
├── modules.py
├── toplevel.py
└── utilities.py
Bu yukarıdaki py
dosyalarının içinde de sırayla aşağıdaki kodlar yer alıyor ve program da run.py
'yi çalıştırınca açılıyor olsun.
Dosya: run.py
#!/usr/bin/python3.8
# -*- coding: utf-8 -*-
if __name__ == "__main__":
from Scripts import main
main()
Dosya: Scripts/modules.py
# -*- coding: utf-8 -*-
import tkinter as tk
from tkinter.messagebox import showwarning
Dosya: Scripts/__init__.py
# -*- coding: utf-8 -*-
from .modules import tk
from .button import Button
from .entry import ManyEntries
from .utilities import open_toplevel
def main():
root = tk.Tk()
login = ManyEntries(
master=root,
texts=["Username", "Password"],
max_chars=[20, 20]
)
apply_button = Button(
master=root,
text="\u2714",
color="green",
command=lambda: open_toplevel(*login.entries.values())
)
root.mainloop()
Dosya: Scripts/utilities.py
# -*- coding: utf-8 -*-
from .toplevel import WelcomeScreen
from .modules import tk, showwarning
def get_entry_values(*entries):
return [ent.get() for ent in entries if isinstance(ent, tk.Entry)]
def has_authentication(*entries):
return get_entry_values(*entries) == ["Admin", "123456"]
def open_toplevel(*entries):
if has_authentication(*entries):
WelcomeScreen(entries[0].get())
else:
showwarning(
title="Warning",
message="Wrong username and password"
)
Dosya: Scripts/toplevel.py
# -*- coding: utf-8 -*-
from .modules import tk
class WelcomeScreen(tk.Toplevel):
def __init__(self, username: str, *args, **kwargs):
super().__init__(*args, **kwargs)
self.label = tk.Label(master=self, text=username)
self.label.pack()
Dosya: Scripts/button.py
# -*- coding: utf-8 -*-
from .modules import tk
class Button(tk.Button):
def __init__(self, color, *args, **kwargs):
super().__init__(*args, **kwargs)
self.configure(
activebackground=self["bg"],
activeforeground=color,
borderwidth=0,
highlightthickness=0,
font="Default 20",
fg=color
)
self.pack()
Dosya: Scripts/entry.py
# -*- coding: utf-8 -*-
from .modules import tk
class Entry(tk.Entry):
def __init__(self, max_char: int, *args, **kwargs):
super().__init__(*args, **kwargs)
self.bind(
sequence="<KeyRelease>",
func=lambda event: self.__apply_max_char(max_char=max_char)
)
self.configure(width=max_char)
if self._name == "password":
self.configure(show="*")
def __apply_max_char(self, max_char):
if len(self.get()) > max_char:
self.delete(max_char, "end")
class ManyEntries(tk.Frame):
def __init__(self, texts: list, max_chars: list, *args, **kwargs):
super().__init__(*args, **kwargs)
self.pack()
self.entries = self.__create_entries(
texts=texts,
max_chars=max_chars
)
def __create_entries(self, texts, max_chars):
entries = {}
for index, (text, max_char) in enumerate(zip(texts, max_chars)):
label = tk.Label(master=self, text=text)
label.grid(row=index, column=0, sticky="w")
entry = Entry(
master=self,
max_char=max_char,
name=text.lower()
)
entry.grid(row=index, column=1, sticky="w")
entries[text] = entry
return entries