python代码中的GUI

时间:2017-06-16 15:58:51

标签: python tkinter

我是编程的初学者,我需要一些帮助,我需要将这个python(3.5.3)代码放在GUI中。我已经阅读了很多tkinter教程,但这些都没有帮助我。 我需要将所有输入和打印出现在屏幕上。 这是代码:

entrada_salario = float(input("Qual o seu  salário mensal? "))

entrada_horas = int(input("Quantas horas você trabalha por dia?"))

entrada_dias = int(input("Quantos dias por semana você trabalha?"))

horas_trabalhadas = ( entrada_horas * entrada_dias ) * 4

valor_hora = entrada_salario / horas_trabalhadas

custo_cem = 100 / valor_hora

print("Você trabalha" , horas_trabalhadas, "h por mês, recebe R$" , valor_hora,"por hora e seu custo 100 é de %.2f" %custo_cem)

1 个答案:

答案 0 :(得分:0)

好的,从我所看到的你只是不了解tkinter或任何GUI的基础知识。话虽如此,你真的应该从教程中构建一些程序,这样你就可以掌握GUI了。

即使是tkinter的基础知识,你想做的事情也很简单。

以下是使用简单的GUI进行此操作的方法。

#import Tkinter as tk # this import is for Python 2.x
import tkinter as tk # this import is for Python 3.x

root = tk.Tk() # this is used to define the tkinter wondow

# each label below is for the user to know what to place in each entry box
label1 = tk.Label(root, text = "Qual o seu  salário mensal? ").grid(row = 0, column = 0)
label2 = tk.Label(root, text = "Quantas horas você trabalha por dia?").grid(row = 1, column = 0)
label3 = tk.Label(root, text = "Quantos dias por semana você trabalha?").grid(row = 2, column = 0)

# each entry box configured to be places next to each label. Simple stuff
entry1 = tk.Entry(root)
entry1.grid(row = 0, column = 1)
entry2 = tk.Entry(root)
entry2.grid(row = 1, column = 1)
entry3 = tk.Entry(root)
entry3.grid(row = 2, column = 1)

# this tkinter text box is where we will print out results
text_box = tk.Text(root, height = 2, width = 100)
text_box.grid(row = 4, column = 0, columnspan = 2)

def do_math(): #this function will only work if every entry has a number in it
# I did not spend the time to write in any error checking as this is just an example of what to do.

    # we can get the values of each entry box with .get()
    # make sure we are working with an integer with the int() function.
    horas_trabalhadas = (int(entry2.get()) * int(entry3.get())) * 4
    valor_hora = int(entry1.get()) / horas_trabalhadas
    custo_cem = 100 / valor_hora
    results = "Você trabalha" , horas_trabalhadas, "h por mês, recebe R$" , valor_hora,"por hora e seu custo 100 é de %.2f" %custo_cem

    # this is the command to print results to the text box.
    text_box.delete(1.0, "end-1c")
    text_box.insert("end-1c", results)
    text_box.see("end-1c")

# this button is configured to call the do_math(): function when it is pressed
submit_button = tk.Button(root, text = "Submit", command = do_math).grid(row = 3)

root.mainloop() # required at then end of your tkinter program