Sunday, May 18, 2025

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple for loop that will spawn 5 instances of blank Tk window

We will import all options from tkinter module because we are using only one module.

If using multiple modules, if we have some big scripts, we can use only specific methods from those modules. With that approach we are evading possible name clashes:


from tkinter import *

for x in range(5):
    Tk()

After using 3 lines from above, if you see all 5 windows on screen, all is ok with import.


Then we can proceed with import of messagebox, alias mb:

import tkinter.messagebox as mb

We need to initialize a blank window with this line:

top_win = Tk()

Then, geometry of a window is set o be 500 x 500:

top_win.geometry("500x500")

Function do_something() is needed to show msg box. Inside msg box, we will have mb activated targeting showinfo() method to show "lorem ipsum stuff" as Title in a window:

def do_something():
    mb.showinfo("Title", "lorem ipsum stuff")

 It's not enough just to have our custom function, we need to activate it on button. A name for button will be but_1. 

That button is located in top_win, and text inside it will be "Yeah". To connect button with function we are using command = do_something, without parenthesis:

but_1 = Button(top_win, text = "Yeah", command = do_something)

Of course, button but_1 must be positioned using coordinates x and y, using method place():

but_1.place(x = 2, y = 2)

Mainloop is needed at the bottom of script:

top_win.mainloop()

This is full script:

from tkinter import *
import tkinter.messagebox as mb

top_win = Tk()
top_win.geometry("500x500")

def do_something():
    mb.showinfo("Title", "lorem ipsum stuff")

but_1 = Button(top_win, text = "Yeah", command = do_something)
but_1.place(x = 2, y = 2)

top_win.mainloop()

MySQL BACKUP, RESTORE Database

You are strongly advised to check corresponding Youtube video.

How to backup MySQL database

  1. Open the Command Prompt or PowerShell on your Windows 10 machine.

  2. Navigate to the directory where the MySQL installation is located. 

  3. Once you are in the MySQL installation directory, execute the following command to backup the database to a SQL file: 

    mysqldump -u [username] -p [database_name] > [backup_file_name.sql]
    

    Replace [username] with your MySQL username, [database_name] with the name of the database you want to backup, and [backup_file_name.sql] with the desired name of the backup file.

    You will be prompted to enter your MySQL password after executing the command.

  4. The backup file will be created in the MySQL installation directory. You can copy the backup file to a different location to store it. 

Backup, Step by Step

We will backup "corp" database.


mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| corp               |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql>

In CMD, go to location where MySQL is installed. 

Microsoft Windows [Version 10.0.18363.959]
(c) 2019 Microsoft Corporation. All rights reserved.

C:\WINDOWS\system32>cd "C:\Program Files\MySQL Server 8.0\bin\"

C:\Program Files\MySQL Server 8.0\bin>mysqldump -u root -p corp > CORPARCH.sql
Enter password: *****

C:\Program Files\MySQL Server 8.0\bin>

cd "C:\Program Files\MySQL Server 8.0\bin\": This command changes the current directory to C:\Program Files\MySQL Server 8.0\bin\ where the mysqldump utility is located.

mysqldump -u root -p corp > CORPARCH.sql: This command runs the mysqldump utility with the username root and the database name corp. It then redirects the output to a file named CORPARCH.sql. This creates a backup of the corp database as a SQL script file.

Enter password: *****: This prompts the user to enter the password for the root user. The password is not visible when typing for security reasons.

C:\Program Files\MySQL Server 8.0\bin>
: This is the command prompt after the mysqldump command has completed. 

C:\Program Files\MySQL Server 8.0\bin>dir CORP*
 Volume in drive C is New Volume

 Directory of C:\Program Files\MySQL Server 8.0\bin

07/24/2020  05:22 PM             5,920 CORPARCH.sql
               1 File(s)          5,920 bytes
               0 Dir(s)  78,735,020,032 bytes free

C:\Program Files\MySQL Server 8.0\bin>
Directory of C:\Program Files\MySQL Server 8.0\bin : This line indicates that the directory of the command prompt is "C:\Program Files\MySQL Server 8.0\bin".

07/24/2020 05:22 PM 5,920 CORPARCH.sql : This line shows the details of the file "CORPARCH.sql" which was created on July 24, 2020 at 5:22 PM and has a size of 5,920 bytes. 

How to Drop and restore MySQL Database 

mysql> DROP DATABASE Corp;
Query OK, 4 rows affected (1.04 sec)

mysql>

The code is dropping the "Corp" database in MySQL. 

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql>

Database "Corp" is no more. 

Restoring Database "Corp"

mysql> CREATE DATABASE Corp;
Query OK, 1 row affected (0.13 sec)

mysql>

Previous code creates a new database called "Corp" in MySQL. 


mysql> USE Corp;
Database changed
mysql> SHOW TABLES FROM Corp;
Empty set (0.00 sec)

mysql>

The code above switches to the database named "Corp" using the command USE Corp.

Then, it tries to display the tables in the currently selected database using the command SHOW TABLES FROM Corp.

Since the database is newly created and no tables have been created yet, the output is an empty set. 

C:\Program Files\MySQL Server 8.0\bin>mysql -u root -p corp < CORPARCH.sql
Enter password: *****

C:\Program Files\MySQL Server 8.0\bin>

This code runs the mysql client from the command line and connects to a MySQL server as the root user, prompting for the user's password.

It then takes a backup file called "CORPARCH.sql" located in the same directory and uses it to restore the "corp" database.

The "<" character is a shell command for input redirection, which means the contents of the backup file will be used as input for the mysql command.

Database Restored 

mysql> SHOW TABLES FROM Corp;
+------------------------+
| Tables_in_corp         |
+------------------------+
| documents              |
| internalcontrol        |
| prices_higher_than_100 |
| stores                 |
+------------------------+
4 rows in set (0.04 sec)

mysql>

Restoring worked. The command SHOW TABLES FROM Corp is used to display all the tables in the "Corp" database. 


mysql> SELECT * FROM Stores;
+------+--------+------------+-----------------+-----------+-------+-----------------+
| s_id | city   | store_name | product         | available | price | ShipCorp        |
+------+--------+------------+-----------------+-----------+-------+-----------------+
|    1 | London | London_1   | Gold PSU        |       153 |   100 | Speedy Gonzales |
|    2 | London | London_2   | Gold PSU        |        75 |   100 | Speedy Gonzales |
|    3 | Berlin | Berlin_1   | Green PSU       |        50 |   120 | Speedy Gonzales |
|    4 | Berlin | Berlin_2   | XYZ Motherboard |         5 |    75 | Speedy Gonzales |
|    5 | Moscow | Moscow_1   | Extension Cable |        50 |    25 | Speedy Gonzales |
|    6 | Moscow | Moscow_2   | LPT Cables      |       500 |    10 | Speedy Gonzales |
|    7 | Miami  | Miami_1    | COM Cables      |      1450 |     5 | Speedy Gonzales |
|    8 | Paris  | Paris_1    | NIC             |       350 |    15 | Speedy Gonzales |
|    9 | XXX    | XXX_1      | Some            |      4562 |   500 | Speedy Gonzales |
|   10 | xxx    | yyy        | ppp             |      5421 |   500 | Speedy Gonzales |
|   11 | bla    | bla        | bla             |      1234 |   350 | Daffy Duck      |
+------+--------+------------+-----------------+-----------+-------+-----------------+
11 rows in set (0.00 sec)

mysql>

All data preserved.

You are strongly advised to check corresponding Youtube video:

List All Files From All Subdirectories - Python


import os

target = 'c:\\Python38-64'

for x in os.walk(target):
    print(x)

The os.walk() function generates the file names in a directory tree by walking the tree either top-down or bottom-up, using a generator. It returns a 3-tuple (dirpath, dirnames, filenames) for each directory in the tree, where dirpath is a string of the path to the directory, dirnames is a list of the names of the subdirectories in dirpath, and filenames is a list of the names of the non-directory files in dirpath.

os.walk(target) is used to generate and print the 3-tuples for each directory in the tree starting at target. The output will show the directory tree structure, with each directory and its subdirectories listed under the preceding directory, and the non-directory files listed under their respective directory. 

import os

target = 'c:\\Python38-64'

for root, dirname, files in os.walk(target):
    for x in files:
        print(x)

This code uses the os module to traverse a directory tree rooted at c:\\Python38-64. The os.walk() function returns a generator object that yields three-tuples for each directory in the tree. Each three-tuple consists of a directory path (root), a list of subdirectory names (dirname), and a list of file names (files) in the directory root.

The for loop iterates over each three-tuple yielded by the os.walk() generator. For each three-tuple, the loop iterates over each file name in the files list and prints it to the console. This effectively prints the name of every file in the c:\\Python38-64 directory tree. 


import os

target = 'c:\\Python38-64'

for root, dirname, files in os.walk(target):
    for x in files:
        print(root + '\\' + x)

This code uses the os.walk() function to iterate through all the directories and files in the specified target directory and its subdirectories.

The os.walk() function returns a generator that produces a 3-tuple on each iteration: the root directory of the current iteration, a list of subdirectory names in the current iteration, and a list of file names in the current iteration.

In this code, we are unpacking the 3-tuple into root, dirname, and files. root is the current root directory being iterated over, dirname is a list of subdirectory names in the current root directory, and files is a list of file names in the current root directory.

The for loop then iterates over each file name in files and prints out the full file path by concatenating root and the file name with the backslash (\\) character used as a directory separator on Windows. The resulting output is a list of all the files in the target directory and its subdirectories with their full paths. 

import os

target = 'c:\\Python38-64'

for root, dirname, files in os.walk(target):
    for x in files:
        if x.endswith('.py'):
            print(root + '\\' + x)

This code uses the os module to traverse the file system starting at the given target directory (c:\Python38-64) and walk through all directories and subdirectories within it. For each file found within this directory and its subdirectories, the code checks whether the filename ends with the extension .py. If the file has this extension, it prints out the full path to the file (using root and x). This code can be useful if you need to find all Python source code files within a particular directory hierarchy.

Saturday, May 17, 2025

CSS Absolute vs Relative

In CSS, position: relative and position: absolute are two properties that define how an element should be positioned on a web page.

When an element is positioned relative, it is positioned relative to its normal position in the document flow. This means that other elements will still take up space around it, even if it is moved. The top, bottom, left, and right properties can be used to move the element in any direction from its normal position.

On the other hand, when an element is positioned absolute, it is taken out of the normal document flow and positioned relative to its closest positioned ancestor (or the <body> element if there is no positioned ancestor). This means that it will not take up any space in the document flow and other elements will be positioned as if it wasn't there. The top, bottom, left, and right properties can also be used to position the element relative to its positioned ancestor.

In summary, position: relative moves the element relative to its normal position in the document flow, while position: absolute takes the element out of the document flow and positions it relative to its closest positioned ancestor. 


<!DOCTYPE html>

<html>
<head><title>Title</title></head>

<link rel="stylesheet" type="text/css" href="mystyle.css">

<body>

<div class="first">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit,
 sed do eiusmod tempor incididunt ut labore et dolore magna
 aliqua. Ut enim ad minim veniam, quis nostrud exercitation
 ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
 
 <div class="second">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit,
 sed do eiusmod tempor incididunt ut labore et dolore magna
 aliqua. Ut enim ad minim veniam, quis nostrud exercitation
 ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
  
</div>
</body>
</html>

Our simple html code. 


.first{
	width: 400px;
	border: 1px solid red;
	
	position: absolute;
	top: 10px;
	left: 25px;
}

.second {
	max-width: 400px;
	margin: auto;
	border: 1px solid red;
	background: yellow;
	
	position: relative;
	bottom: 0px;
	left: 0px;		
}

This CSS code defines two classes .first and .second with different positioning properties:

  • .first is positioned absolutely with top: 10px and left: 25px. This means that its position is relative to the first positioned ancestor element, or to the initial containing block if there is no such ancestor. The element is taken out of the normal flow of the document and the surrounding content will be adjusted to fill in the gap left by the element.
  • .second is positioned relatively with bottom: 0px and left: 0px. This means that its position is relative to its normal position in the document flow. The element is still part of the normal flow of the document, but its position can be adjusted relative to where it would normally appear.

By applying these classes to elements in an HTML document, you can control their position on the page in different ways. In the HTML code we provide, the .first class is applied to a div element, while the .second class is applied to a nested div element. This will cause the two elements to be positioned differently relative to each other.

Here's an example HTML code with internal CSS that demonstrates the difference between absolute and relative positioning: 

<!DOCTYPE html>
<html>
<head>
	<title>Positioning Example</title>
	<style>
		.first {
			position: relative;
			border: 1px solid red;
			height: 200px;
			width: 200px;
			margin: 50px;
			background-color: #f0f0f0;
		}
		
		.second {
			position: absolute;
			top: 50px;
			left: 50px;
			border: 1px solid blue;
			height: 100px;
			width: 100px;
			background-color: #c0c0c0;
		}
	</style>
</head>
<body>

	<div class="first">
		<p>This is the parent element with relative position.</p>
		
		<div class="second">
			<p>This is the child element with absolute position.</p>
		</div>
	</div>

</body>
</html>

In this example, the .first element is positioned relatively, meaning that its child element .second will be positioned absolutely in relation to its boundaries. The .second element is then positioned at top: 50px; and left: 50px; from the top-left corner of its parent .first element.

Friday, May 2, 2025

Remote Administration Tool - Python - File Upload - [ Part 8 ]

Remote Administration Tool - Python - File Download - [ Part 7 ]

Remote Administration Tool - Python - Remove Directory - [ Part 6 ]

Remote Administration Tool - Python - TXT Reports and Directory Creation - [ Part 5 ]

Remote Administration Tool - Python - Change Directory - [ Part 4 ]

Remote Administration Tool - Python - Restrictions - [ Part 3 ]

Remote Administration Tool - Python - Subprocess Module - [ Part 2 ]

Remote Administration Tool - Python - Server and Client - [ Part 1 ]

Python Remote Administration

  1. Remote Administration Tool - Python - Server and Client - [ Part 1 ]
  2. Remote Administration Tool - Python - Subprocess Module - [ Part 2 ]
  3. Remote Administration Tool - Python - Restrictions - [ Part 3 ]
  4. Remote Administration Tool - Python - Change Directory - [ Part 4 ]
  5. Remote Administration Tool - Python - TXT Reports and Directory Creation - [ Part 5 ]
  6. Remote Administration Tool - Python - Remove Directory - [ Part 6 ]
  7. Remote Administration Tool - Python - File Download - [ Part 7 ]
  8. Remote Administration Tool - Python - File Upload - [ Part 8 ]

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...