added webpack and fixed ImageLabel
This commit is contained in:
@@ -159,7 +159,6 @@ async function generateTkinterCode(projectName, widgetList=[], widgetRefs=[], as
|
||||
})
|
||||
}
|
||||
|
||||
// TODO: add empty __init__ file
|
||||
for (let customWidget of customPythonWidgets){
|
||||
|
||||
let [fileName, extension] = customWidget.split(".")
|
||||
@@ -169,9 +168,8 @@ async function generateTkinterCode(projectName, widgetList=[], widgetRefs=[], as
|
||||
}
|
||||
|
||||
|
||||
const fileContent = pythonFiles(`./${fileName}`);
|
||||
const fileContent = pythonFiles(`./${fileName}`).default
|
||||
|
||||
console.log("file name: ", fileContent.default, pythonFiles(`./${fileName}`))
|
||||
createFileList.push({
|
||||
fileData: new Blob([fileContent], { type: "text/plain" }),
|
||||
fileName: fileName,
|
||||
@@ -179,6 +177,14 @@ async function generateTkinterCode(projectName, widgetList=[], widgetRefs=[], as
|
||||
})
|
||||
}
|
||||
|
||||
if (customPythonWidgets.length > 0){
|
||||
createFileList.push({
|
||||
fileData: new Blob([''], { type: "text/plain" }),
|
||||
fileName: '__init__.py',
|
||||
folder: "customWidgets"
|
||||
})
|
||||
}
|
||||
|
||||
for (let asset of assetFiles){
|
||||
|
||||
if (asset.fileType === "image"){
|
||||
|
||||
@@ -1,26 +1,43 @@
|
||||
# Author: Paul: https://github.com/PaulleDemon
|
||||
# Made using PyUibuilder: https://pyuibuilder.com
|
||||
# MIT License - keep the copy of this license
|
||||
|
||||
import tkinter as tk
|
||||
from PIL import Image, ImageTk
|
||||
|
||||
|
||||
class ImageLabel(tk.Label):
|
||||
def __init__(self, master, image_path, mode="fit", *args, **kwargs):
|
||||
def __init__(self, master, image_path=None, mode="cover", width=100, height=100, *args, **kwargs):
|
||||
"""
|
||||
mode:
|
||||
mode:
|
||||
- "fit" -> Keeps aspect ratio, fits inside label
|
||||
- "cover" -> Covers label fully, cropping excess
|
||||
"""
|
||||
super().__init__(master, *args, **kwargs)
|
||||
self.parent = master # Store parent reference
|
||||
super().__init__(master, width=width, height=height, *args, **kwargs)
|
||||
self.parent = master
|
||||
self.image_path = image_path
|
||||
self.mode = mode
|
||||
self.original_image = Image.open(image_path)
|
||||
self.original_image = None
|
||||
self.photo = None
|
||||
self.resize_job = None # Debounce job reference
|
||||
|
||||
self.force_resize() # Initial resize
|
||||
self.after(100, self.init_events) # Delay event binding slightly
|
||||
if mode not in ['fit', 'cover']:
|
||||
raise Exception("Mode can only be fit or cover.")
|
||||
|
||||
if image_path:
|
||||
try:
|
||||
self.original_image = Image.open(image_path)
|
||||
self.photo = ImageTk.PhotoImage(self.original_image)
|
||||
self.config(image=self.photo)
|
||||
|
||||
self.force_resize()
|
||||
except Exception as e:
|
||||
print(f"Error loading image: {e}")
|
||||
|
||||
self.after(100, self.init_events)
|
||||
|
||||
def init_events(self):
|
||||
self.parent.bind("<Configure>", self.on_resize) # Bind resize to parent
|
||||
self.parent.bind("<Configure>", self.on_resize)
|
||||
|
||||
def on_resize(self, event=None):
|
||||
"""Debounce resizing to prevent rapid execution."""
|
||||
@@ -30,8 +47,13 @@ class ImageLabel(tk.Label):
|
||||
|
||||
def force_resize(self):
|
||||
"""Resize image using actual widget size."""
|
||||
width = self.winfo_width()
|
||||
height = self.winfo_height()
|
||||
|
||||
if self.original_image is None:
|
||||
return # Do nothing if no image is loaded
|
||||
|
||||
width = self.winfo_width()
|
||||
height = self.winfo_height()
|
||||
|
||||
if width < 5 or height < 5:
|
||||
return
|
||||
|
||||
@@ -53,6 +75,7 @@ class ImageLabel(tk.Label):
|
||||
else:
|
||||
new_width = int(height * aspect_ratio)
|
||||
new_height = height
|
||||
|
||||
resized = self.original_image.resize((new_width, new_height), Image.LANCZOS)
|
||||
|
||||
# Crop excess
|
||||
|
||||
@@ -561,7 +561,8 @@ export class TkinterBase extends Widget {
|
||||
const { side = "top", expand = false, anchor } = widgetRef.getPackAttrs() || {};
|
||||
|
||||
// console.log("rerendering:", side, expand);
|
||||
|
||||
|
||||
|
||||
const directionMap = {
|
||||
top: "column",
|
||||
bottom: "column-reverse",
|
||||
@@ -587,7 +588,7 @@ export class TkinterBase extends Widget {
|
||||
expandValue = 1
|
||||
}
|
||||
|
||||
// TODO: if previous expand value is greater than 0 and current doesn't have expand then it should be 0
|
||||
// TODO: if the child widget as fillx or y use flex grow
|
||||
|
||||
if ((expand && !isSameSide) || (expand && previousExpandValue === 0)){
|
||||
previousExpandValue = expandValue;
|
||||
@@ -610,7 +611,10 @@ export class TkinterBase extends Widget {
|
||||
|
||||
|
||||
const stretchClass = isVertical ? "tw-flex-grow" : "tw-h-full"; // Allow only horizontal growth for top/bottom
|
||||
|
||||
// TODO: if previous expand value is greater than 0 and current doesn't have expand then it should be 0
|
||||
|
||||
// const fill = this.getAttrValue("flexManager.fillX") || this.getAttrValue("flexManager.fillY")
|
||||
|
||||
if (isSameSide) {
|
||||
return (
|
||||
<>
|
||||
|
||||
@@ -12,7 +12,7 @@ class Label extends TkinterWidgetBase{
|
||||
static widgetType = "label"
|
||||
static displayName = "Label"
|
||||
|
||||
static requiredCustomPyFiles = ["imageLabel"]
|
||||
// static requiredCustomPyFiles = ["imageLabel"]
|
||||
|
||||
constructor(props) {
|
||||
super(props)
|
||||
@@ -104,11 +104,20 @@ class Label extends TkinterWidgetBase{
|
||||
const imports = super.getImports()
|
||||
|
||||
if (this.getAttrValue("imageUpload"))
|
||||
imports.push("import os", "from PIL import Image, ImageTk", )
|
||||
imports.push("import os", "from PIL import Image, ImageTk", "from customWidgets.imageLabel import ImageLabel")
|
||||
|
||||
return imports
|
||||
}
|
||||
|
||||
getRequiredCustomPyFiles(){
|
||||
const requiredCustomFiles = super.getRequiredCustomPyFiles()
|
||||
|
||||
if (this.getAttrValue("imageUpload"))
|
||||
requiredCustomFiles.push("imageLabel")
|
||||
|
||||
return requiredCustomFiles
|
||||
}
|
||||
|
||||
getRequirements(){
|
||||
const requirements = super.getRequirements()
|
||||
|
||||
@@ -161,10 +170,10 @@ class Label extends TkinterWidgetBase{
|
||||
const code = []
|
||||
|
||||
if (image?.name){
|
||||
code.push(`${variableName}_img = Image.open(${getPythonAssetPath(image.name, "image")})`)
|
||||
code.push(`${variableName}_img = ImageTk.PhotoImage(${variableName}_img)`)
|
||||
// code.push(`${variableName}_img = Image.open(${getPythonAssetPath(image.name, "image")})`)
|
||||
// code.push(`${variableName}_img = ImageTk.PhotoImage(${variableName}_img)`)
|
||||
// code.push("\n")
|
||||
labelInitialization = `${variableName} = tk.Label(master=${parent}, image=${variableName}_img, text="${labelText}", compound=tk.TOP)`
|
||||
labelInitialization = `${variableName} = ImageLabel(master=${parent}, image_path=${getPythonAssetPath(image.name, "image")}, text="${labelText}", compound=tk.TOP, mode="${this.getAttrValue("imageSize.mode")}")`
|
||||
}
|
||||
|
||||
// code.push("\n")
|
||||
|
||||
Reference in New Issue
Block a user