# -*- coding: utf-8 -*-
# bla bla bla
"""
@author: ***
"""
# problems
"""
сделать:
1. сохранение результатов, DataProjectDialog?
2. обработчик результатов, ActProjectDialog?
3. че с кодировкой utf-8? кракозяблы?!
-- ага ini файл должен быть в кодеровке системы
(windows cp-1251, linux utf-8) или перекодировать
в системную кодировку в
зависимости от используеой системы
"""
# example ini file
"""
[start]
#default path
default_path='/'
#title window
title=test
#text on the button 0
btn_0= < prev
#text on the button 1
btn_1= select
#text on the button 2
btn_2= next >
#first part of the text of a label
label=step
#regular expression for named
re=[a-zA-Zа-яА-Я0-9_-]
# еще ... хз возожно указать файл | код обработчик ?
[0]
# str, int, float, bin
type=bin
# on|off (0|1) str filter
named=0
#prev, select, next, text
state_default=0100
state_use=0110
# question text
text=select mode (True - one, False - two)
# step to prev
prev=0
# step to next (False, True (1, 2) or Data (1))
next=1,2
# askdirectory, askyesnocancel, askstring, askfloat, askinteger, askopenfile
ask_type=askyesnocancel
ask_title='?'
ask_msg='select mode\nyes - True\nno - False'
[1]
type=str
named=1
state_default=1100
state_use=1110
text=entrer str
prev=0
next=2
ask_type=askstring
ask_title='?'
ask_msg='entrer str'
[2]
type=str
named=0
state_default=1100
state_use=1110
text=select file
prev=0
next=1
ask_type=askopenfile
ask_title='?'
ask_msg='file'
"""
# code
import tkinter as tk
from tkinter import scrolledtext
from tkinter.messagebox import askyesnocancel
from tkinter.simpledialog import askstring
from tkinter.simpledialog import askfloat
from tkinter.simpledialog import askinteger
from tkinter.filedialog import askdirectory
from tkinter.filedialog import askopenfile
import time
import configparser
import re
class MasterProjectDialog:
# giu, ini
def __init__(self, ini=r''):
self.step_count = 0
#---
self.ini = ini
self.config = configparser.ConfigParser()
self.config.read(self.ini)
self.title = self.config.get('start', 'title')
self.btn_0 = self.config.get('start', 'btn_0')
self.btn_1 = self.config.get('start', 'btn_1')
self.btn_2 = self.config.get('start', 'btn_2')
self.label = self.config.get('start', 'label')
self.path = self.config.get('start', 'default_path')
self.re = self.config.get('start', 're')
#---
self.root = tk.Tk()
self.root.resizable(width=False, height=False)
self.root.title(self.title)
#---
self.t_str = tk.StringVar(master=self.root)
#---
self.f = tk.Frame(master=self.root, height=300, width=400)
self.f.grid(column=0, row=0)
self.f.pack_propagate(True)
self.f.pack()
#---
self.l = tk.Label(master=self.f)
self.l.grid(sticky='w', column=0, row=0)
self.l.pack_propagate(True)
#---
self.t = scrolledtext.ScrolledText( master=self.f, wrap = tk.WORD)
self.t.grid(column=0, row=1)
self.t.pack_propagate(True)
self.t.configure(state='disabled')
#---
self.b0 = tk.Button(
master=self.f, text=self.btn_0, command=self.b0_cmd)
self.b0.grid(sticky='w', column=0, row=2)
self.b0.configure(state='disabled')
self.b0.pack_propagate(True)
#---
self.b1 = tk.Button(
master=self.f, text=self.btn_1, command=self.b1_cmd)
self.b1.grid(column=0, row=2)
self.b1.pack_propagate(True)
#---
self.b2 = tk.Button(
master=self.f, text=self.btn_2, command=self.b2_cmd)
self.b2.grid(sticky='e' , column=0, row=2)
self.b2.pack_propagate(True)
#---
self.result = None
self.results = {}
self.b012 = [0, 0, 0]
self._state()
self._update_gui()
def b0_cmd(self):
self.b012 = [1, 0, 0]
self.runner()
self._state()
def b1_cmd(self):
self.b012 = [0, 1, 0]
self.runner()
def b2_cmd(self):
self.b012 = [0, 0, 1]
self.runner()
self._state()
def _update_gui(self):
txt = '\n'.join([self.config.get(
str(self.step_count), "text"), str(self.result)])
self.t.configure(state='normal')
self.t.delete('1.0', tk.END)
self.t.insert(tk.INSERT, txt)
self.t.configure(state='disabled')
self.root.after(250, self._update_gui)
def _named(self, name):
t = re.compile(self.re)
return "".join([ch for ch in name if t.match(ch)])
def runner(self):
#--- b0_cmd
if self.b012.index(1) == 0:
self._btn0run()
#--- b1_cmd
elif self.b012.index(1) == 1:
self._btn1run()
#-check-----------------------------------------------------------
self.check()
#-----------------------------------------------------------------
#--- b2_cmd
elif self.b012.index(1) == 2:
self._btn2run()
def _btn0run(self):
self.step_count = int(self.config.get(
str(self.step_count), "prev"))
def _btn1run(self):
cmd = self.config.get(str(self.step_count), "ask_type")
title = self.config.get(str(self.step_count), "ask_title")
msg = self.config.get(str(self.step_count), "ask_msg")
if cmd == 'askdirectory' or cmd == 'askopenfile':
self.result = eval(
f'{cmd}(initialdir={self.path}, title={title})')
if cmd == 'askopenfile':
try:
self.result = self.result.name
except AttributeError:
self.result = None
else:
self.result = eval(f'{cmd}({title}, {msg})')
if self.config.get(str(self.step_count), "type") == 'str':
if int(self.config.get(str(self.step_count), "named")):
try:
self.result = self._named(self.result)
except TypeError:
self.result = None
if self.result == '':
self.result = None
self._pre()
def _btn2run(self):
if self.config.get(str(self.step_count), "type") == 'bin':
nexts = self.config.get(
str(self.step_count), "next").split(',')
if self.result:
self.step_count = nexts[0]
else:
self.step_count = nexts[1]
else:
self.step_count = int(self.config.get(
str(self.step_count), "next"))
def _state(self):
self.l['text'] = f"{self.label} {self.step_count}"
self.b012 = [0, 0, 0]
self.b0.configure(state='normal' if int(list(self.config.get(str(
self.step_count), "state_default"))[0]) else 'disabled')
self.b1.configure(state='normal' if int(list(self.config.get(str(
self.step_count), "state_default"))[1]) else 'disabled')
self.b2.configure(state='normal' if int(list(self.config.get(str(
self.step_count), "state_default"))[2]) else 'disabled')
self.result = None
def _pre(self):
if self.result != None:
self.b0.configure(state='normal' if int(list(
self.config.get( str(
self.step_count), "state_use"))[0]
) else 'disabled')
self.b1.configure(state='normal' if int(list(
self.config.get(str(
self.step_count), "state_use"))[1]
) else 'disabled')
self.b2.configure(state='normal' if int(
list(self.config.get(str(
self.step_count), "state_use"))[2]
) else 'disabled')
else:
self._state()
#-check-------------------------------------------------------------------
def check(self):
DataProjectDialog.data.append(DataProjectDialog.test)
DataProjectDialog.test += 1
DataProjectDialog().get_data()
ActProjectDialog().act_this(DataProjectDialog.data)
#-------------------------------------------------------------------------
#-to-make---------------------------------------------------------------------
class DataProjectDialog: # check
# save
data = []
test = 0
def get_data(cls):
print('data', cls.data)
class ActProjectDialog: # check
# actions
def act_this(cls, xz):
print('act ', xz)
#-----------------------------------------------------------------------------
class D1MoK(MasterProjectDialog):
# наследуем MasterProjectDialog и певеопредеяем действие кнопки prev
def __init__(self, ini=r''):
super().__init__(ini)
def b0_cmd(self):
if self.result == '1':
tk.messagebox.showinfo(f'ответ {self.step_count}', 'правильно')
else:
tk.messagebox.showinfo(f'ответ {self.step_count}', 'не правильно')
def main(ini):
#MasterProjectDialog(ini).root.mainloop()
D1MoK(ini).root.mainloop()
if __name__ == "__main__":
main('prttp2.ini')
1. смешивать разноязычные коментарии не очень
3. MasterProjectDialog().check() просто демонстрация работы с классами DataProjectDialog, ActProjectDialog которые в общем то не более чем тестовые
4. хз … может еще че