我有一些代码可以很好地使标签式图片查看器正常工作,只有在分别设置每个图像的路径时,标签查看器才具有适当数量的标签。我希望将所有图像保存在一个文件夹中,而不必为每个图像写一行,
pathToFile1 = (r'C:\Users\...\Desktop\one.jpg')
pathToFile2 = (r'C:\Users\...\Desktop\two.jpg')
pathToFile3 = (r'C:\Users\...\Desktop\three.jpg')
然后再使用
def setupImages(self):
return [
Image.FromFile(pathToFile1),
Image.FromFile(pathToFile2),
Image.FromFile(pathToFile3),
]
然后使用以下功能...
def setupPanel(self, parent):
tabControl = TabControl()
tabControl.Dock = DockStyle.Fill
tabControl.Alignment = TabAlignment.Bottom
for i in range(3):
tabPage = TabPage()
tabPage.Text = 'Image %s' % i
tabPage.Controls.Add(self.getPictureBox(self.images[i])
tabControl.TabPages.Add(tabPage)
parent.Controls.Add(tabControl)
使用Artog的setupImages函数的完整代码。
import clr
clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Drawing')
from os import listdir
from os.path import isfile, join
from System.Drawing import Image
from System.Windows.Forms import (
Application, DockStyle, Form, Orientation, PictureBox,
PictureBoxSizeMode, SplitContainer,
TabAlignment, TabControl, TabPage
)
class MainForm(Form):
def __init__(self):
Form.__init__(self)
self.images = self.setupImages()
splitter = SplitContainer()
splitter.Orientation = Orientation.Vertical
splitter.Dock = DockStyle.Fill
def SwapOrientation(sender, event):
if sender.Orientation == Orientation.Vertical:
sender.Orientation = Orientation.Horizontal
else:
sender.Orientation = Orientation.Vertical
splitter.DoubleClick += SwapOrientation
self.setupPanel(splitter.Panel1)
self.setupPanel(splitter.Panel2)
self.Controls.Add(splitter)
self.Text = 'Xaar Rogues Gallery'
self.Show()
def setupImages(self):
mypath = (r'C:\Users\priper\Desktop\RoguesGalleryImages') # No
need to wrap with ()
valid_file_endings = ['jpg','png','jpeg'] # Add / remove as needed
# Best to start variable names with lower case, so as not to
confuse with classes
image_filenames = [
Image.FromFile(f) for f in listdir(mypath)
if isfile(join(mypath, f))
and f[-3:] in valid_file_endings # check if last three
characters of filename is in the list of valid endings
]
# print len(image_filenames)
# print(image_filenames)
return image_filenames
def getPictureBox(self, image):
pictureBox = PictureBox()
pictureBox.SizeMode = PictureBoxSizeMode.StretchImage
pictureBox.Image = image
pictureBox.Dock = DockStyle.Fill
return pictureBox
def setupPanel(self, parent):
tabControl = TabControl()
tabControl.Dock = DockStyle.Fill
tabControl.Alignment = TabAlignment.Bottom
for i in range(3):
tabPage = TabPage()
tabPage.Text = 'Image %s' % i
tabPage.Controls.Add(self.getPictureBox(self.images[i]))
tabControl.TabPages.Add(tabPage)
parent.Controls.Add(tabControl)
Application.EnableVisualStyles()
form = MainForm()
Application.Run(form)
用于自动获取文件夹中图像的代码,而无需单独进行硬编码,如果有一种自动使用图像名称来命名选项卡的方法,那就太好了。
答案 0 :(得分:1)
您已经在代码中包含了解决方案,如注释中所述。只需将逻辑移到函数内部,就可以完成:)
def setupImages(self):
mypath = 'C:\Users\...\Desktop\RoguesGalleryImages' # No need to wrap with ()
valid_file_endings = ['jpg','png','jpeg'] # Add / remove as needed
# Best to start variable names with lower case, so as not to confuse with classes
image_filenames = [
Image.FromFile(f) for f in listdir(mypath)
if isfile(join(mypath, f))
and f[-3:] in valid_file_endings # check if last three characters of filename is in the list of valid endings
]
# print len(image_filenames)
# print(image_filenames)
return image_filenames