繁体   English   中英

Python3 上的 tkinter 问题:发生在 Mac OS X 上,而不是 Windows 10

[英]tkinter issue on Python3: happens on Mac OS X, not on Windows 10

正如标题所说,相同的代码,相同的 Python 3 版本在两台计算机上,一台运行 Mac OSX,另一台运行 Win10。

我完全不知道为什么会发生这种情况,我真的很感激对这里发生的事情的一些解释。 我对 python 编码并不陌生,但我对 tkinter 和 GUI 设计(大部分)缺乏经验。

PS.:它在 Windows 10 上运行良好(即,输出我想要的),但它不会在 Mac OSX 上运行(即,在 GUI 上点击“运行”按钮显然没有任何作用;缺少输入文本框?)。

谢谢!

这是我在 Win 10 上得到的这是我在 Win 10 上得到的

这是我在 Mac OSX 上得到的macOS

这是我的代码:

import tkinter as tk
import math as m
from tkinter import *

root= tk.Tk()
root.title('Cloze Generator')


frame = Frame(root)
frame.pack()

freq = Label(frame, text='Convert every how many words:')
freq.pack( side = LEFT )
#canvas.create_window(50, 100, window=freq)

fr = StringVar()
entry1 = Entry(frame, textvariable=fr)
entry1.pack( side = RIGHT)

canvas = tk.Canvas(root, width = 800, height = 700)
canvas.pack()

entry = tk.Text(root, height=15, width=90) 
canvas.create_window(400, 200, window=entry)

def cloze ():  
    x1 = entry.get('1.0','end')
    originalText = x1

    text = originalText.split()
    code = '{1:SHORTANSWER:='
    newText = ''
    i = 0
    fr = int(entry1.get())

    for word in text:
        i += 1
        if i % fr == 0:
            if word[-1] == '.':
                wordDot = word[:-1] 
                if len(wordDot) > 1: 
                    newWord = wordDot[:m.floor(len(word)/2)]
                    answer = wordDot[m.floor(len(word)/2):]
                    outWord = newWord + code + answer + '}'
                    newText += outWord + '. '
                else:
                    newText += word + '. '
            elif word[-1] == ',':
                wordDot = word[:-1] 
                if len(wordDot) > 1: 
                    newWord = wordDot[:m.floor(len(word)/2)]
                    answer = wordDot[m.floor(len(word)/2):]
                    outWord = newWord + code + answer + '}'
                    newText += outWord + ', '
                else:
                    newText += word + ', '        
            else:
                if len(word) > 1: 
                    newWord = word[:m.floor(len(word)/2)]
                    answer = word[m.floor(len(word)/2):]
                    outWord = newWord + code + answer + '}'
                    newText += outWord + ' '
                else:
                    newText += word + ' '
        else:
            newText += word + ' '

    outText = tk.Text(root, height=15, width=90)
    outText.insert(tk.END, newText)
    canvas.create_window(400, 500, window=outText)

button = tk.Button(text='Run', command=cloze)
canvas.create_window(50, 350, window=button)


root.mainloop()

文本小部件没有丢失。 它在那里,但它没有边框,并且它的背景与其父背景相同,因此它与背景融为一体。

不同的平台对小部件有不同的默认值。 在 OSX 上,文本小部件的默认浮雕是“flat”,边框宽度是 0,而在 windows 上,它是“sunken”和 1。

要查看小部件,如果您希望它在两个平台上看起来相同,您可以显式设置边框宽度和浮雕:

entry = tk.Text(root, height=15, width=90, bd=1, relief="sunken")

PS.:它在 Windows 10 上运行良好(即,输出我想要的),但它不会在 Mac OSX 上运行(即,在 GUI 上点击“运行”按钮显然没有任何作用;缺少输入文本框?)。

我无法复制这种行为。 当我在输入小部件中插入文本并输入数字后单击运行按钮时,function 似乎可以工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM