首页 \ 问答 \ 如何在Swift中设置UILabel的textColor(How to set textColor of UILabel in Swift)

如何在Swift中设置UILabel的textColor(How to set textColor of UILabel in Swift)

当我尝试使用代码将UILabel的颜色设置为另一个UILabel的颜色

myLabel.textColor = otherLabel.textColor

它不会改变颜色。 然而,当我使用这个代码时,

myLabel.textColor = UIColor.redColor()

它正确地改变颜色。 第一行有什么问题?


When I try setting the color of a UILabel to the color of another UILabel using the code

myLabel.textColor = otherLabel.textColor

It doesn't change the color. When I use this code, however,

myLabel.textColor = UIColor.redColor()

It changes the color correctly. What's the issue with the first line?


原文:https://stackoverflow.com/questions/25837228
更新时间:2023-07-31 09:07

最满意答案

我希望我的问题是正确的。 你必须在每一步都检查球员的位置。 我建议您在每次移动时定义区域(例如go_right ),您需要检查玩家是否在这些边界内。 如果是True ,则可以加载另一个脚本。 这是一个快速而肮脏的解决方案:

def go_fwd(event):
    areal.focus_set()
    me.configure(me.x, me.y-2)
    check_if_player_enters_region(me)

def go_bwd(event):
    areal.focus_set()
    me.configure(me.x, me.y+2)
    check_if_player_enters_region(me)

def go_left(event):
    areal.focus_set()
    me.configure(me.x-2, me.y)
    check_if_player_enters_region(me)

def go_right(event):
    areal.focus_set()
    me.configure(me.x+2, me.y)
    check_if_player_enters_region(me)

def check_if_player_enters_region(player):
    ## Define your Areas here
    if(is_in_region(player, 20, 20, 10, 10)):
        print "enter region 1, load script xxx"
    if(is_in_region(player, 50, 50, 10, 10)):
        print "enter region 2, load script xxx"   

    playarea.create_rectangle(20,20,30,30, fill='blue') ## this is just for visualisation, delete this line
    playarea.create_rectangle(50,50,60,60, fill='blue') ## this is just for visualisation, delete this line

def is_in_region(player, x, y, width, height): # define region boundries
    if((player.x >= x and player.x <= x+width) and  ## check x boundries
       (player.y >= y and player.y-2 <= y+height)): ## check y boundries
        return True
    return False

dw = tk.tk()
dw.title('Erebos')

me = player(0,0)

areal = tk.Frame(master=dw, width=20000, height=600, bg='black')
areal.pack_propagate(0)
areal.pack(fill=tk.BOTH, expand=bool(dw)-100)
areal.bind("<1>", lambda event: areal.focus_set())
dw.bind("<Up>", go_fwd)
dw.bind("<Down>", go_bwd)
dw.bind("<Left>", go_left)
dw.bind("<Right>", go_right)

playarea = tk.Canvas(width=800, height=500, master=areal, bg='white')
playarea.pack()
playarea.create_rectangle(20,20,30,30, fill='blue') ## this is just for visualisation, delete this line
playarea.create_rectangle(50,50,60,60, fill='blue') ## this is just for visualisation, delete this line
me.zeichne(playarea)
dw.mainloop()

我只检查播放器的中心,通常你会检查播放器的“盒子”。


I hope i get your question right. You have to check the players position on every move. I recommend that you define areas, on every move (e.g. go_right) you need to check if the player is in those boundaries. If that is True you can load another script. Here is a quick and dirty solution:

def go_fwd(event):
    areal.focus_set()
    me.configure(me.x, me.y-2)
    check_if_player_enters_region(me)

def go_bwd(event):
    areal.focus_set()
    me.configure(me.x, me.y+2)
    check_if_player_enters_region(me)

def go_left(event):
    areal.focus_set()
    me.configure(me.x-2, me.y)
    check_if_player_enters_region(me)

def go_right(event):
    areal.focus_set()
    me.configure(me.x+2, me.y)
    check_if_player_enters_region(me)

def check_if_player_enters_region(player):
    ## Define your Areas here
    if(is_in_region(player, 20, 20, 10, 10)):
        print "enter region 1, load script xxx"
    if(is_in_region(player, 50, 50, 10, 10)):
        print "enter region 2, load script xxx"   

    playarea.create_rectangle(20,20,30,30, fill='blue') ## this is just for visualisation, delete this line
    playarea.create_rectangle(50,50,60,60, fill='blue') ## this is just for visualisation, delete this line

def is_in_region(player, x, y, width, height): # define region boundries
    if((player.x >= x and player.x <= x+width) and  ## check x boundries
       (player.y >= y and player.y-2 <= y+height)): ## check y boundries
        return True
    return False

dw = tk.tk()
dw.title('Erebos')

me = player(0,0)

areal = tk.Frame(master=dw, width=20000, height=600, bg='black')
areal.pack_propagate(0)
areal.pack(fill=tk.BOTH, expand=bool(dw)-100)
areal.bind("<1>", lambda event: areal.focus_set())
dw.bind("<Up>", go_fwd)
dw.bind("<Down>", go_bwd)
dw.bind("<Left>", go_left)
dw.bind("<Right>", go_right)

playarea = tk.Canvas(width=800, height=500, master=areal, bg='white')
playarea.pack()
playarea.create_rectangle(20,20,30,30, fill='blue') ## this is just for visualisation, delete this line
playarea.create_rectangle(50,50,60,60, fill='blue') ## this is just for visualisation, delete this line
me.zeichne(playarea)
dw.mainloop()

I only check the center of the player, normally you do check the "box" of the player.

相关问答

更多
  • 假设你的意思是你的应用程序窗口,当你说“我的其他窗口”时,你可以在Toplevel或Tk上使用lift()方法: root.lift() 如果您希望窗口保持在所有其他窗口之上,请使用: root.attributes("-topmost", True) root是你的Toplevel还是Tk。 不要忘记- "topmost" ! 要使其成为临时状态 ,请在以下位置禁用最上层: def raise_above_all(window): window.attributes('-topmost', 1 ...
  • 有几个选项可以提供给Label小部件,如您在此处所看到的 。 您应该在名称后面指定所有参数: partNumberInfo = tkinter.Label(window, text=PartNumber) 要在Tkinter中使用Python变量,需要特殊的Tk对象,其中有四个: BooleanVar , DoubleVar , IntVar和StringVar 。 看到这个答案是一个很好的例子。 There are several options you can give to a Label widg ...
  • 我希望我的问题是正确的。 你必须在每一步都检查球员的位置。 我建议您在每次移动时定义区域(例如go_right ),您需要检查玩家是否在这些边界内。 如果是True ,则可以加载另一个脚本。 这是一个快速而肮脏的解决方案: def go_fwd(event): areal.focus_set() me.configure(me.x, me.y-2) check_if_player_enters_region(me) def go_bwd(event): areal.focu ...
  • 你不能有两个Tk实例。 对于第二个窗口,您需要创建一个Toplevel 。 您也不应该在整个程序中多次调用mainloop 。 You can't have two instances of Tk. For your second window you need to create a Toplevel. You also should never call mainloop more than once in your entire program.
  • 由于递归引起的崩溃可能是因为你的on_resize方法创建了新的小部件。 这是发生了什么: 小部件获得一个...... 调用on_resize ...... 创建一个嵌套循环...... 创建一个画布,其中...... 导致窗口小部件中的配置更改 你在嵌套循环中调用update ... 导致事件被处理,... 调用on_resize,... 创建一个嵌套循环...... 创建一个画布...... 导致窗口小部件中的配置更改 你在嵌套循环中调用update ... 导致事件被处理,... 调用on_resiz ...
  • 您可以使用grid来实现这一点,方法是定义放置小部件的row : from tkinter import Tk, Canvas, Button CHANGED = False def options(): button1 = Button(window, text='Click to change door') button1.bind("", change) button1.grid(row=0, sticky='w') button2 = Butt ...
  • 当Entrybox_002函数返回时, stringvar1被垃圾收集(删除)。 解决方法: stringvar1 = window2.stringvar1 = tk.StringVar(value='asdf') 我建议您将Entrybox_002编码为类,并将stringvar1编码为实例属性以保持参考。 stringvar1 is garbage collected (deleted) when Entrybox_002 function return. Workaround: stringvar1 ...
  • 在mainloop运行后,你的代码是正确的。 它确实如此,但只有在GUI被销毁之后。 Tkinter设计用于调用mainloop是最后一行(或几乎最后一行)的可执行代码。 一旦调用,所有其他工作必须作为对事件的反应。 这是GUI编程的本质。 “如何更改变量”的答案很简单:在调用mainloop之前执行此mainloop ,或者在响应事件时执行此操作。 例如,在回调按钮时执行此操作,在绑定到事件的函数中执行此操作,或通过after ,依此类推。 You are correct that code after ...
  • 您应该将Label(s)放在与您有按钮相同的Frame内。 当我复制你的代码时,我还必须导入filedalog模块( from tkinter import filedalog ),星号导入似乎没有涵盖它。 您可以使用StringVar()变量(来自tkinter)并将其分配给标签。 这个变量你可以从.set()和.get()值。 创建变量并将其分配给Label: self.value = StringVar('', value=" Load GCODE file to find volume, \n w ...
  • 您无法获取列表变量,因为窗口小部件对列表变量一无所知。 创建窗口小部件时,您将从列表窗口小部件中提供值 ,但不是列表本身。 menu属性返回Menu类的实例。 它也不会对你的清单有任何了解。 如果要将列表与窗口小部件关联,请将其设置为属性: my_opmenu.list = list1 之后,当您想要列表时,您可以执行以下操作: print "my list:", my_opmenu.list You cannot get the list variable because the widget kno ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)