Home
Categories
WIKI
Topic
User
LANGUAGE:
中文
English
用gtk.fixed来简单的固定布局.
社区开发
1626
views ·
0
replies ·
To
floor
Go
z85525006
deepin
2012-09-03 17:51
Author
由于我上一次太急了,写了很多垃圾代码,经过我好好的设想后,我感觉GTK FIXED在窗口不能变大变小的情况下,是非常简约的代码,其中我想到初中的数学,坐标系....
|(y)
|
------------------------------------------------------------------------------------(x)
| button_1(0, 1) button_right_1(1, 1)
| button_2(0, 2) button_right_2(1, 2)
| button_3(0, 3) button_right_3(1, 3)
|
|
|
|
不知道大家发现没有,只需要一个循环就能将这些控件对齐起来.
widgets_left = [gtk.Button("控件一"),
gtk.Button("控件二"),
gtk.Button("控件三"),
gtk.Button("控件四"),]
widgets_right = [gtk.Button("控件5"),
gtk.Button("控件7"),
gtk.Button("控件123"),
]
widgets_all = [widgets_left, widgets_right]
widgets_offset_x = 0
widgets_offset_y = 0
widgets_all = map(lambda left,right:(left,right), widgets_left, widgets_right)
widgets_offset_x += 120
for widget_left, widget_right in widgets_all:
# print widget_left, widget_right
# print "========="
widgets_offset_x -= 120
widgets_offset_y += 50
if widget_left:
self.fixed.put(widget_left, widgets_offset_x, widgets_offset_y) # add left widget.
widgets_offset_x += 120
if widget_right:
self.fixed.put(widget_right, widgets_offset_x, widgets_offset_y) # add right widget.
Copy the Code
跟随控件的增加,代码越来越简洁, 你将那个代码封转成一个函数,通过的调用,就OK啦.这个是测试代码,不要罗嗦.
再则这个情况用法只能用于 全部控件都是X坐标对齐的. y无所谓,y可以无限的扩大.
要是害怕界面真的被扩大,可以加上窗体的坐标, 然后就不怕位置变化了.
谢谢.... 一起学习吧.这是一个gtk.fixed的书用技巧.
以后代码要好好想,才能动手写,
封成函数的代码:
def all_widget_to_widgets(widget_left, widget_right):
return map(lambda left, right:(left, right), widget_left, widget_right)
def widgets_add_widget(fixed, widgets_left, widgets_right, start_x, start_y, offset_x, offset_y):
widgets_all = all_widget_to_widgets(widgets_left, widgets_right)
widgets_offset_x = start_x
widgets_offset_y = start_y
for widget_left, widget_right in widgets_all:
widgets_offset_x = start_x
if widget_left:
fixed.put(widget_left, widgets_offset_x, widgets_offset_y) # add left widget.
widgets_offset_x += offset_x
if widget_right:
fixed.put(widget_right, widgets_offset_x, widgets_offset_y) # add right widget.
# y positon move widget_left and widget_right.
widgets_offset_y += offset_y
我的测试代码:
widgets_add_widget(self.fixed, widgets_label_left, widgets_label_right, start_x, start_y, offset_x, offset_y)
widgets_add_widget(self.fixed, widgets_left, widgets_right, start_x, start_y + 20, offset_x, offset_y)
现在的代码只需要改变 start_x, start_y, offset_x, offset_y..全部需要对齐的控件都马上对齐起来,代码更简洁,更容易修改了.
不用为了改界面的X,Y,去改动多处代码.
Copy the Code
Reply
Like 0
Favorite
View the author
All Replies
No replies yet
Please
sign
in first
New Thread
Popular Ranking
Change
Calender app not opening.
Popular Events
More
|(y)
|
------------------------------------------------------------------------------------(x)
| button_1(0, 1) button_right_1(1, 1)
| button_2(0, 2) button_right_2(1, 2)
| button_3(0, 3) button_right_3(1, 3)
|
|
|
|
不知道大家发现没有,只需要一个循环就能将这些控件对齐起来.
再则这个情况用法只能用于 全部控件都是X坐标对齐的. y无所谓,y可以无限的扩大.
要是害怕界面真的被扩大,可以加上窗体的坐标, 然后就不怕位置变化了.
谢谢.... 一起学习吧.这是一个gtk.fixed的书用技巧.
以后代码要好好想,才能动手写,
封成函数的代码: