jeudi 17 février 2022

Print result displayed in a textbox of an external window, inside the textobox of the main window (using the button of the main window)

In connection with this question solved by the kind acw1668, I place an executable code because I would simply like to display in the main window the result of a textbox that is generated in another external window. enter image description here

Basically there is the main file main.py that opens the file page1.py inside it. Inside page1 there is a label on which you click on and a new window opens called windows_option.py where the checkboxes, functions and buttons of the aforementioned question are present. Everything ok, no problem. Works well. If you click the Print button, things print correctly.

WHAT AM I TRYING TO DO? What I am trying to do, however, is not to print the results in the windows_option.py textbox, but simply print them in the page1.py textbox. Consequently I would like to delete the Print button in windows_option.py and use only the Print button of page1.py using it when I close the windows_option.py window. For the rest, I would like to keep the windows_option.py file as it is, with functions inside, etc. etc.

Of course, vote and answer accepted with a green tick for those who help me :)

Main.py

import tkinter as tk
from tkinter import ttk
from PIL import ImageTk
from page1 import Page1

root = tk.Tk()
root.geometry('480x320')

topbar = tk.Frame(root, bg='#e10a0a', height=43)
topbar.pack(fill='x')

style = ttk.Style()
style.theme_use('default') # select a theme that allows configuration of ttk.Notebook
# put the tabs at the left with white background
style.configure('TNotebook', tabposition='wn', background='white', tabmargins=0)
# configure tab with white background initially, yellow background when selected
style.configure('TNotebook.Tab', background='white', width=10, focuscolor='yellow', borderwidth=0)
style.map('TNotebook.Tab', background=[('selected', 'yellow')])

nb = ttk.Notebook(root)
nb.pack(fill='both', expand=1)


page1 = Page1(nb)

nb.add(page1, text='aaaaa', compound='left')

root.mainloop()

Page1.py

import tkinter as tk
from tkinter import ttk
from tkinter import *
from tkinter import ttk
import tkinter as tk
import tkinter.font as tkFont
from tkinter import ttk

from Folder1.Folder2 import namefile

import windows_option
from windows_option import form


class Page1(tk.Frame):
    def __init__(self, master, **kw):
        super().__init__(master, **kw)


        #TEXTOBOX
        textbox = tk.Text(self, width=33, height=10, font=('helvetic', 12))
        textbox.place(x=30, y=40)

        #BUTTON
        button = tk.Button(self, text="Print", command= lambda: [aaa()])
        button.place(x=10, y=10)
        
        option= Label(self, text="Option", bg="#960000", foreground='white', font='Ubuntu 10')
        option.place(x=100, y=10)
        option.bind("<Button-1>", lambda event: windows_option.form(self))

windows_option.py

import sqlite3

from tkinter import *
from tkinter import ttk
import tkinter as tk
import tkinter.font as tkFont
from tkinter import ttk
from PIL import ImageTk


import tkinter.messagebox
from tkinter import messagebox


def form(parent):
    opz = tk.Toplevel(parent)
    opz.title("Option")
    opz.geometry("500x400")
    opz.config(bg="white")
    opz.state("normal")
    opz.transient(parent)



    class Option1(tk.Frame):
        def __init__(self, master, **kw):
            super().__init__(master, **kw)
            self.configure(bg='white')



            chk_lst = []
            fn_lst = [] 
            funclist = set() 

            Checkbutton1 = tk.IntVar()
            Checkbutton2 = tk.IntVar()
            Checkbutton3 = tk.IntVar()

            #CHECKBOX'S FUNCTIONS
            def Button1_func():
                if 5 + 3 == 8:
                    return True
                else:
                    return False

            def Button2_func():
                if 5 + 3 == 7:
                    return True
                else:
                    return False

            def Button3_func():
                if 5 + 3 == 8:
                    return True
                else:
                    return False

            def clicked(flag, func):
                if flag:
                    funclist.add(func)
                else:
                    funclist.remove(func)


            #CHECKBOX
            Button1 = tk.Checkbutton(self, text = "Checkbox 1", variable = Checkbutton1, onvalue = 1, offvalue = 0, height = 1,
                                     bg="white", foreground='black', activebackground="white",
                                     command=lambda: clicked(Checkbutton1.get(), Button1_func))
            Button1.place(x=10, y=36)

            Button2 = tk.Checkbutton(self, text = "Checkbox 2", variable = Checkbutton2, onvalue = 1, offvalue = 0, height = 1,
                                     bg="white", foreground='black', activebackground="white",
                                     command=lambda: clicked(Checkbutton2.get(), Button2_func))
            Button2.place(x=10, y=66)

            Button3 = tk.Checkbutton(self, text = "Checkbox 3", variable = Checkbutton3, onvalue = 1, offvalue = 0, height = 1,
                                     bg="white", foreground='black', activebackground="white",
                                     command=lambda: clicked(Checkbutton3.get(), Button3_func))
            Button3.place(x=10, y=146)

            chk_lst.extend([Checkbutton1, Checkbutton2, Checkbutton3])
            fn_lst.extend([Button1_func, Button2_func, Button3_func])


            #SAVE IN DATABASE
            def save():
                c.execute('INSERT INTO table1 VALUES (?, ?)', (idx, chk_btn.get()))
                c = conn.cursor()

                for idx,chk_btn in enumerate(chk_lst,start=1):
                    c.execute('UPDATE table1 SET Button1 = ? WHERE id = ?', (chk_btn.get(), idx))
                    rec = c.fetchall()

                    if rec:
                        c.execute("UPDATE table1 SET Button1=? WHERE id=?;", (chk_btn.get(),idx))
                    else:
                        c.execute("INSERT INTO table1 VALUES (?,?,?);", (idx,chk_btn.get()))
                    
                conn.commit()
                conn.close()

                messagebox.showinfo('SAVE', 'Saved successfully')


            #LOAD WHEN OPEN WINDOWS
            def load():
                conn = sqlite3.connect("value.db")
                c = conn.cursor()
                c.execute("SELECT * FROM table1")
                vals = c.fetchall()

                for val, chk_btn, func in zip(vals, chk_lst, fn_lst):
                    chk_btn.set(val[1])
                    
                    if val[1] == '1': 
                        funclist.add(func)

                conn.close()


            #BUTTON FUNCTION

            def aaa():
                # if need to clear the text box, uncomment below line
                #textbox.delete("1.0", "end")
                if funclist and all(func() for func in funclist): 
                    textbox.insert("end", "Ok")


            #SAVE BUTTON
            save = tk.Button(self, text="Save", bg='#b40909', foreground='white', command= save)
            save.place(x=10, y=10)

            #TEXTOBOX
            textbox = tk.Text(self, width=33, height=10, font=('helvetic', 12))
            textbox.place(x=10, y=220)

            #PRINT BUTTON
            button = tk.Button(self, text="Print", command= lambda: [aaa()])
            button.place(x=100, y=10)

            load()

    topbar = tk.Frame(opz, bg='#e10a0a', height=50)
    topbar.pack(fill='x')

    style = ttk.Style()
    #style.theme_use('clam') # select a theme that allows configuration of ttk.Notebook
    # put the tabs at the left with white background
    style.configure('TNotebook', tabposition='wn', background='#e10a0a', tabmargins=0)
    # configure tab with white background initially, yellow background when selected
    style.configure('TNotebook.Tab', background='#e10a0a', width=25, focuscolor='#e10a0a',  foreground='white', borderwidth=1)
    style.map('TNotebook.Tab', background=[('selected', '#b40909')])


    nb = ttk.Notebook(opz_gol)
    nb.pack(fill='both', expand=1)

    img = ImageTk.PhotoImage(file='/img/.....')
    img2 = ImageTk.PhotoImage(file='/img/.....')
    img3 = ImageTk.PhotoImage(file='/img/.....')
    img4 = ImageTk.PhotoImage(file='/img/.....')
    img5 = ImageTk.PhotoImage(file='/img/.....')

    page1 = Option1(nb)

    nb.add(page1, text='Option 1', image=img, compound='left')

    opz.mainloop()

Simple database:

CREATE TABLE "table1" (
    "id"    INTEGER,
    "Button1"   TEXT,
    PRIMARY KEY("id" AUTOINCREMENT)
);



Aucun commentaire:

Enregistrer un commentaire